如何使用java中的接口作为列表或数组类型?
大家好,这里的聪明人 我做了关于这个主题的研究,我知道它写在某个地方,但我在任何地方都找不到它(但我知道我几周前读到过它)。。。所以我的问题是: 在Java8中,应该可以使用接口作为数组或列表中的集合类型(不确定是哪一种)。。。你们中有人能告诉我这个功能的名称吗?或者给我一个抽象或清晰的代码示例 据我所知,这个例子是这样的:您有不同类型的对象,它们共享同一个接口。有了这个接口,就可以创建一个列表或数组来存储不同的对象类型 如果有人能帮我,我会很高兴的。。。我真的很喜欢我在那里读到的东西,但现在我有一些业余时间,找不到我从哪里得到这些信息 提前谢谢你如何使用java中的接口作为列表或数组类型?,java,Java,大家好,这里的聪明人 我做了关于这个主题的研究,我知道它写在某个地方,但我在任何地方都找不到它(但我知道我几周前读到过它)。。。所以我的问题是: 在Java8中,应该可以使用接口作为数组或列表中的集合类型(不确定是哪一种)。。。你们中有人能告诉我这个功能的名称吗?或者给我一个抽象或清晰的代码示例 据我所知,这个例子是这样的:您有不同类型的对象,它们共享同一个接口。有了这个接口,就可以创建一个列表或数组来存储不同的对象类型 如果有人能帮我,我会很高兴的。。。我真的很喜欢我在那里读到的东西,但现在我
//编辑:谢谢你的快速回答,Real怀疑论者 这实际上与Java8无关。如果您有不同的对象,它们都实现了相同的接口,我们将其称为
MyInterface
,那么您可以创建如下数组:
MyInterface[] myArray = new MyInterface[size];
List<MyInterface> theList = new ArrayList<MyInterface>();
您可以将这些对象指定给该数组的元素。当然,您只能使用MyInterface
定义中或Object
定义中的方法,除非您将成员强制转换回其原始类中
列表也是如此。您可以定义一个列表:
List<MyInterface> list = new ArrayList<MyInterface>();
List List=new ArrayList();
然后,您可以将实现
MyInterface
的任何对象添加到此列表中,当您从该列表中获取成员时,您可以对其执行的操作具有相同的限制:只有MyInterface
和object
的方法,除非您回过头来看。我认为您可能在考虑泛型,从第5版开始就使用Java。假设您有一个名为MyInterface的接口,以及实现该接口的任意数量的类,例如MyClassOne和MyClassII。可以创建一个集合(如列表),该集合将接受实现MyInterface的任何类,如下所示:
MyInterface[] myArray = new MyInterface[size];
List<MyInterface> theList = new ArrayList<MyInterface>();
以下操作将产生编译错误:
theList.add("A String");
从列表返回的对象将是MyInterfaces。您不会立即知道它是MyClassOne还是MyClassII:
MyInterface obj = theList.get(0);
if (obj instanceof MyClassOne) {
System.out.println("It's MyClassOne");
} else if (obj instanceof MyClassTwo) {
System.out.println("It's MyClassTwo");
}
阵列的情况与此类似——任何MyInterface实现类都可以进入,MyInterface实例也可以出来:
MyInterface[] array = { new MyClassOne(), new MyClassTwo() };
MyInterface obj = array[0];
if (obj instanceof MyClassOne) {
System.out.println("It's MyClassOne");
} else if (obj instanceof MyClassTwo) {
System.out.println("It's MyClassTwo");
}
这是阵列的完整示例:
interface Foo {
String bar();
}
class Impl1 implements Foo {
String bar() {
return "This is implementation 1";
}
}
class Impl2 implements Foo {
String bar() {
return "This is implementation 2";
}
}
public class Main {
public static void main(String[] args) {
Foo[] myArray = new Foo[2];
myArray[0] = new Impl1();
myArray[1] = new Impl2();
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i].bar());
}
}
}
您正在考虑的是Java泛型
public interface MyInterface { public void foo();}
public class Class1 implements MyInterface {public void foo() {System.out.println("1");}}
public class Class2 implements MyInterface {public void foo() {System.out.println("2");}}
public class Main {
public static void main(String[] args) {
List<MyInterface> list = new ArrayList<>();
list.add(new Class1());
list.add(new Class2());
list.get(0).foo(); // Prints 1
list.get(1).foo(); // Prints 2
Class1 c1 = (Class1) list.get(0); // OK
c1 = (Class1) list.get(1); // Runtime error - ClassCastException
}
public接口MyInterface{public void foo();}
公共类Class1实现MyInterface{public void foo(){System.out.println(“1”);}
公共类Class2实现MyInterface{public void foo(){System.out.println(“2”);}
公共班机{
公共静态void main(字符串[]args){
列表=新的ArrayList();
添加(新的Class1());
添加(新类2());
list.get(0.foo();//打印1
list.get(1.foo();//打印2
Class1C1=(Class1)list.get(0);//确定
c1=(Class1)list.get(1);//运行时错误-ClassCastException
}
此外,还可以按如下方式使用通配符:
public static void main(String[] args) {
List<Class1> list1 = foo1(); // Compiler error - incompatible types
List<Class1> list2 = (List<Class1>) foo2(); // Warning - unchecked cast
Class1 class1 = list2.get(0); // OK
List<Object> list3 = (List<Object>) foo1();
Class2 class2 = (Class2) list3.get(0); // Runtime error - ClassCastException
}
private static List<? super MyInterface> foo1() {
List<Object> list = new ArrayList<>();
list.add(new Class1());
return list;
}
private static List<? extends MyInterface> foo2() {
List<? extends MyInterface> list1 = new ArrayList<Class1>();
list1.add(new Class1()); // Compiler error: the compiler is not aware of the list's runtime type
// from the compiler's point of view, list1 could have runtime type List<Class2>
List<Class1> list = new ArrayList<>();
list.add(new Class1());
return list;
}
publicstaticvoidmain(字符串[]args){
List list1=foo1();//编译器错误-不兼容类型
List list2=(List)foo2();//警告-未选中强制转换
Class1=list2.get(0);//确定
List list3=(List)foo1();
Class2 Class2=(Class2)list3.get(0);//运行时错误-ClassCastException
}
私有静态列表