当前位置: 技术问答>java相关
How to create an object of a protected inner class of another package?
来源: 互联网 发布时间:2015-08-18
本文导语: ------The environment variable CLASSPATH=E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsminec08 ------E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsmineC08package1A4.java: package package1; public interface A4 { void a4(); } ------E:materialmi...
------The environment variable CLASSPATH=E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsminec08
------E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsmineC08package1A4.java:
package package1;
public interface A4 {
void a4();
}
------E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsmineC08package2B4.java:
package package2;
import package1.*;
public class B4 {
protected class C4 implements A4 {
public void a4() { System.out.println("a4()"); }
}
}
------E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsmineC08package3E12_InnerClass.java:
package package3;
import package1.*;
import package2.*;
public class E12_InnerClass extends B4 {
E12_InnerClass ic=new E12_InnerClass();
A4 ic() {
return ic.new C4();
}
public static void main(String[] args) { }
}
------First,class C4 is inherited to class E12_InnerClass.
Second,I use an object of the outer class to make an object of the inner class.But there is a compile-time error "
E12_InnerClass.java:10: C4() has protected access in package2.B4.C4
return ic.new C4();
^
1 error "
The result appears even when "E12_InnerClass ic=new E12_InnerClass();" is wiped out and "return ic.new C4();" is turned into "return new C4();".
What is the fault?How to get an object of C4 in the method ic?
Thanks a lot.
------E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsmineC08package1A4.java:
package package1;
public interface A4 {
void a4();
}
------E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsmineC08package2B4.java:
package package2;
import package1.*;
public class B4 {
protected class C4 implements A4 {
public void a4() { System.out.println("a4()"); }
}
}
------E:materialminejavaenglishTIJ-2nd-editionexercise-solutionsmineC08package3E12_InnerClass.java:
package package3;
import package1.*;
import package2.*;
public class E12_InnerClass extends B4 {
E12_InnerClass ic=new E12_InnerClass();
A4 ic() {
return ic.new C4();
}
public static void main(String[] args) { }
}
------First,class C4 is inherited to class E12_InnerClass.
Second,I use an object of the outer class to make an object of the inner class.But there is a compile-time error "
E12_InnerClass.java:10: C4() has protected access in package2.B4.C4
return ic.new C4();
^
1 error "
The result appears even when "E12_InnerClass ic=new E12_InnerClass();" is wiped out and "return ic.new C4();" is turned into "return new C4();".
What is the fault?How to get an object of C4 in the method ic?
Thanks a lot.
|
这个问题真的很有意思,我特意调试了一下,发现了存在问题的地方
inner class C4 的权限是protected,说明它在被继承时是可见的,
这没有什么可说的,如果C4不是inner class的话,编译是完全通过的。
问题在return ic.new C4();这一句,在这里我们调用了C4的default constructor,但它的权限却是default,那就意味着除了package2以外
都无法调用该constructor,解决方法很简单,就是声明C4()为public
或是protected,这样就可见了。你同意我的说法吗?
inner class C4 的权限是protected,说明它在被继承时是可见的,
这没有什么可说的,如果C4不是inner class的话,编译是完全通过的。
问题在return ic.new C4();这一句,在这里我们调用了C4的default constructor,但它的权限却是default,那就意味着除了package2以外
都无法调用该constructor,解决方法很简单,就是声明C4()为public
或是protected,这样就可见了。你同意我的说法吗?
|
这个你protected访问权限有关系,用他定义的方法或者类,只能被他所在的包,或他的子类所在的包调用。
|
改成public是一种方法,另一种方法是把package都去掉,或者让所有的类都在同一个package里。