2014-12-05 55 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

相关问题