how to call inner class#39;s method from static main() method(如何从静态 main() 方法调用内部类的方法)
问题描述
尝试在父类中创建 1 个接口和 2 个具体类.这将使封闭类成为内部类.
Trying to create 1 interface and 2 concrete classes inside a Parent class. This will qualify the enclosing classes to be Inner classes.
public class Test2 {
interface A{
public void call();
}
class B implements A{
public void call(){
System.out.println("inside class B");
}
}
class C extends B implements A{
public void call(){
super.call();
}
}
public static void main(String[] args) {
A a = new C();
a.call();
}
}
现在我不太确定如何在静态 main() 方法中创建类 C 的对象并调用类 C 的 call() 方法.现在我遇到问题了:A a = new C();
Now I am not really sure how to create the object of class C inside the static main() method and call class C's call() method.
Right now I am getting problem in the line : A a = new C();
推荐答案
这里的内部类不是静态的,所以需要创建外部类的实例,然后调用new,
Here the inner class is not static, so you need to create an instance of outer class and then invoke new,
A a = new Test2().new C();
但在这种情况下,您可以将内部类设为静态,
But in this case, you can make the inner class static,
static class C extends B implements A
那么就可以使用了,
A a = new C()
这篇关于如何从静态 main() 方法调用内部类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从静态 main() 方法调用内部类的方法
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01