假设我有:
public class A {
public A() {
...
}
...
public class B {
public B() {
...
}
public void doSomething() {
...
}
...
}
public class C {
public C() {
...
}
public void doSomething() {
...
}
...
}
}
如果我想创建一个可以同时包含 B 和 C 的 ArrayList,以便我可以在 A 内部调用 myArray.get(i).doSomething()
,我会选择什么类型想要声明我的 ArrayList?
最佳答案
List<myInterface>
。您还需要 B
的接口(interface)和C
:
interface myinterface {
void doSomething();
}
两者都是B
和C
必须implement myInterface
.
关于Java - 外部类内部的内部类数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7671851/