[英].class file of static nested class on compiling Outer class can't be used via association but can be used via inheritance. why?
I have an outer class and a static nested class as 我有一个外部类和一个静态嵌套类作为
class Outer {
static int x = 20;
static class Nested {
static void show() {
System.out.println(x);
}
}
}
on compiling this program ,I've got two .class files named: Outer.class and Outer$Nested.class . 在编译该程序时,我有两个名为.out的.class文件:Outer.class和Outer $ Nested.class。
Now my question is ,I am trying to use the Outer$Nested.class in my other class Test1 (using association). 现在我的问题是,我正在尝试在其他类Test1中使用Outer $ Nested.class(使用关联)。
class Test1 {
public static void main(String... args) {
Outer$Nested i = new Outer$Nested();
}
}
It gives me compile time error : can't find symbol Outer$Nested. 它给了我编译时错误:找不到符号Outer $ Nested。 And I know we can use static nested class directly as Outer.Nested, but I am just trying this.
而且我知道我们可以将静态嵌套类直接用作Outer.Nested,但我只是在尝试这一点。 But when I am trying to use this class via Inheritance then it is compiling and running fine.
但是,当我尝试通过继承使用此类时,它可以编译并运行良好。
class Temp2 extends Outer$Inner {
public static void main(String... args) {
show();
}
}
Now compiling and running fine , output : 20 现在编译并运行正常,输出:20
So my question is why it is happening that we aren't able to use Outer$Nested via association and event we can't declare variable , but can use via inheritance ? 所以我的问题是,为什么不能通过关联和事件使用Outer $ Nested,而不能声明变量,但是可以通过继承使用呢?
I didn't know how the inheritance concepts works like your way, but want to create instance of the inner nested class use this lines of codes 我不知道继承概念的工作方式与您的方式相同,但想使用此行代码创建内部嵌套类的实例
public static void main(String[] args){
Outer.Nested obj= new Outer.Nested();
obj.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.