2014-12-05 57 views
-1

(聲明:我是一個很長一段時間的程序員,只是我沒那麼在使用Java的。)Java接口,通用的,不知道我在做什麼

大家好。

我剛剛爲我的Android應用程序添加了一項新功能,一切正常,但我不知道我做了什麼:-D和Eclipse給了我三個警告。我恨有未解決的警告:-)

的代碼,第一:

public interface IClass<T> { 
    public ArrayList<T> getSomething(); 
    [a few other public methods, not relying on T] 
} 

public class Class1 implements IClass<String>{ 
    public ArrayList<String> getSomething(){ 
     [code] 
    } 

    [the other public methods] 
} 

public class Class2 implements IClass<long>{ 
    public ArrayList<long> getSomething(){ 
     [code] 
    } 

    [the other public methods] 
} 

然後我有一個三等功,稱之爲ConsumerClass:聲明類型的變量的iCLASS,與如果在特定條件下將其實例化爲新的Class1()新的Class2(),在其餘的代碼中它作爲通用的IClass在需要的地方, getSomething(),做一個演員。

正如我所說,一切都按預期工作。 「問題」在於Eclipse給了我3個警告,並不是真的擅長Java,我想了解它們,如果我所做的是正確的,因爲畢竟我編寫了這個東西......以及本能。

警告是:

  1. ICLASS是原始類型。引用泛型類型的iCLASS應該是參數
  2. 類型安全:未選中從ArrayList中轉換爲ArrayList的
  3. 類型安全:未選中從ArrayList中轉換爲ArrayList的

首先,它是完全黑暗的我。 最後兩句我可以理解,只有我不知道如何檢查通話結果而不做。我的意思是:

ArrayList<String> res = (ArrayList<String>) IClass_Instance.getSomething(); 

如何檢查IClass_Instance.getSomething()的類型不調用呢?

THX所有:-)

+0

什麼是IClass_Instance?這個名字表明它是一個變量(所以它的類型是什麼?),但名稱以大寫字母開頭,該大寫字母是Java編碼約定爲類型保留的。 – 2014-12-05 14:55:40

+0

我測試過你的代碼,並且沒有編譯器問題。請提供必要的代碼來複制您的問題,否則該問題不適用於該網站的主題。 – 2014-12-05 14:57:26

+0

'IClass '是非法的 - 你只能使用對象類型 - 試試'IClass '。 – OldCurmudgeon 2014-12-05 14:59:20

回答

0

因爲你不提供您的iCLASS類型變量聲明,我會假設你做這種方式:

Iclass iClass_Instance = new Class1(); 

即使你打電話Class1()構造函數,你變量仍然有IClass類型。因此,當您調用getSomething()方法時,編譯器不知道<T>是哪種類型,因爲您沒有在變量類型聲明中指定它,因此是警告。

你應該聲明的變量是這樣的:

IClass<String> iClass_Instance = new Class1(); 

這樣編譯器知道你的iCLASS實例型的iCLASS的事實,不管你調用的構造函數。

編輯:

我不知道域名,但你可能採取了錯誤的設計決策。這聽起來像典型的情況,你需要接口層次結構。喜歡的東西:

public interface ParametrizedInterface<T> { 
    public List<T> getParametrizedList(); 
} 

public interface StringInterface extends ParametrizedInterface<String> {} 

public interface IntegerInterface extends ParametrizedInterface<Integer> {} 

而且實現

public class ParametrizedImplementation<T> implements ParametrizedInterface<T> { 

    private List<T> elements; 

    public ParametrizedImplementation() { 
     elements = new ArrayList<T>(); 
    } 

    @Override 
    public List<T> getParametrizedList() { 
     return this.elements; 
    } 
} 

public class RightStringImplementation implements StringInterface { 

    List<String> elements; 

    public RightStringImplementation() { 
     elements = new ArrayList<String>(); 
    } 

    @Override 
    public List<String> getParametrizedList() { 
     return this.elements; 
    } 
} 

public class StringImplementation implements ParametrizedInterface<String> { 

    private List<String> elements; 

    public StringImplementation() { 
     elements = new ArrayList<String>(); 
    } 

    public List<String> getParametrizedList() { 
     return this.elements; 
    } 
} 

public class IntegerImplementation implements ParametrizedInterface<Integer> { 

    private List<Integer> elements; 

    public IntegerImplementation() { 
     elements = new ArrayList<Integer>(); 
    } 

    public List<Integer> getParametrizedList() { 
     return this.elements; 
    } 

} 

有了這個,你已經真正類型安全

public class HierarchyTest { 

    public static void main(String[] args) { 

     // You could do things like this 
     ParametrizedInterface testObj = new StringImplementation(); 

     // This compiles because you don't specify the type of the parameter 
     // when you declare the variable. 
     List<Integer> elements = testObj.getParametrizedList(); 
     // ... 

     // You should do things like this 
     // With the parametrized implementation 
     ParametrizedInterface<String> strObj = new ParametrizedImplementation<String>(); 

     List<String> strElements = strObj.getParametrizedList(); 
     // ... 

     // With the hierarchy of interfaces 
     StringInterface rightStrObj = new RightStringImplementation(); 

     List<String> rightStrElements = rightStrObj.getParametrizedList(); // This is right 
     //List<Integer> wrongIntElements = rightStrObj.getParametrizedList(); // This does not compile because of type safety 
    } 
} 

與您最初的方法,你可能有類型的問題如果你不小心,就會投。通過propper界面層次結構,您可以獲得類型安全性的好處。

另請注意,我將您的示例變量名稱更改爲iClass_Instance。按照慣例,Java中的變量名以小寫字符開頭。

+0

對不起,我的錯。 IClass_Instance用於「IClass類型的變量的實例」。 – motoDrizzt 2014-12-05 15:55:01

+0

現在...對不起,重新讀你回覆我完全失去了。如果在聲明它時限制變量是特定類型的變量,那麼在實現接口時有什麼意義?做你的意思是我必須聲明一個變量IClass ,一個IClass ,然後用IF填充代碼,每次我需要調用其中一個Interface方法來知道我需要使用哪兩個對象時..也就是說,它完全消除了抽象變量類型的接口有用性,但仍然確保它們實現特定的方法。我錯了嗎? – motoDrizzt 2014-12-05 16:09:21

+0

我編輯了答案,刪除了不正確的信息並添加了一個工作代碼示例。 – 2014-12-05 18:09:48

相關問題