【发布时间】:2022-01-22 23:40:16
【问题描述】:
我有这个代码:
public class test{
private testInterface i1;
public test(testInterface i1) {
this.i1 = i1;
}
test() {
}
interface testInterface {
}
class inner1 implements testInterface {
}
class inner2 implements testInterface {
}
class inner3 implements testInterface {
}
public void determineClass_Interfac(){
}
}
public class main {
public static void main(String[] args) {
test test1 =new test(new test().new inner1());
}
}
我不知道这是否可行,但我想要实现的是通过调用方法 determineClass_Interfac() 来确定创建了哪个内部类对象。因此,它应该依赖于在类构造函数中创建的对象(这里是 innerClass1)。 我的方法可能完全错误,我只是在这里试验。
【问题讨论】:
-
这里的目标是什么?假设你发现了,有什么用?
-
使用 instanceof 操作符。
-
如果您只需要知道某个内部(或其他)类是否实现了接口,您可以使用
instanceof。例如:inner1 test1 = new test().new inner1(); System.out.println(test1 instanceof testInterface);
标签: java class interface inner-classes