如何访问一个受保护的静态内部类?
public class A{
protected static class AInner{
public void test(){
}
}
}
public class B extends A{
public void hello(){
//怎样才能访问AInner中的test方法呢?
}
}
为什么当A和B在不同包中时,new AInner().test()
会编译报错呢?
而当A和B在同一个包中,却不会报错?
A
B
Different packages will report errors because the constructor of A.AInner is the default constructor, and the default access level allows children of the same package Class use's.A
B
不同包会报错是因为 A.AInner的构造器是默认构造器,而默认访问级别是允许同包的子类使用的。所以如果你可以改A类那么你可以写个 portected或者public级别的构造器。
如果你不改
If you don’t change theA
So if you can change class A then you can write a portected or public level constructor.A
class, you can take a look at this.I wrote an accessible demo.
STRUCTURE
Category A:
#🎜🎜#Category B: #🎜🎜# rrreeeWhy?
protected
The visibility range is determined by the visibility range, visible within packages and inherited classes.How to access? If you must force access, just modify the visibility through reflection.