本文介绍了如何从静态main()方法调用内部类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试在Parent类中创建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()方法调用内部类的方法
发布评论