如何访问受保护的内部 class 外部包?
How to access protected inner class outside package?
package p1;
public class demo1{
public static void main(String []args){
}
protected class demo12{
protected int fun2(int a,int b){
return a*b;
}
}
package p2;
import p1.demo1;
public class demo2{
public static void main(String []args){
//access fun2 method here.
}
}
我创建了一个包 p1
并创建了一个内部 class 名称 demo12
。我想访问包 p2
中的 demo12
fun2
方法。怎么做?
对 class demo2
进行任何更改。我想直接或间接地从 class demo2
访问该方法,而不更改 demo1
的代码。
您的 class demo2
应该继承 demo1
:
class demo2 extends demo1 {...}
为了从 demo2
class 访问您的 demo12
内部 class,demo2
class 必须扩展 demo1
.此外,内部classes与class的实例相连,因此您不能直接从静态方法中调用它。
之间的区别
Static Nested Classes
As with class methods and variables, a static nested class is
associated with its outer class. And like static class methods, a
static nested class cannot refer directly to instance variables or
methods defined in its enclosing class: it can use them only through
an object reference.
Inner Classes
As with instance methods and variables, an inner class is associated
with an instance of its enclosing class and has direct access to that
object's methods and fields. Also, because an inner class is
associated with an instance, it cannot define any static members
itself.
已编辑:
我建议检查一下您的设计是否 class 层次结构无法简化。特别关注内部嵌套和静态嵌套 classes.
之间的区别
你的内部 class demo12
也被标记为受保护,你也可以扩展它。检查这个技巧:
package p2;
import p1.demo1;
public class demo2 extends demo1 {
protected int foo( int a, int b ) {
return (new demo22()).fun2(a, b);
}
protected class demo22 extends demo12 {
protected int fun2( int a, int b ) {
return super.fun2(a, b);
}
}
public static void main( String[] args ) {
(new demo2()).foo(2, 3);
}
}
package p1;
public class demo1{
public static void main(String []args){
}
protected class demo12{
protected int fun2(int a,int b){
return a*b;
}
}
package p2;
import p1.demo1;
public class demo2{
public static void main(String []args){
//access fun2 method here.
}
}
我创建了一个包 p1
并创建了一个内部 class 名称 demo12
。我想访问包 p2
中的 demo12
fun2
方法。怎么做?
对 class demo2
进行任何更改。我想直接或间接地从 class demo2
访问该方法,而不更改 demo1
的代码。
您的 class demo2
应该继承 demo1
:
class demo2 extends demo1 {...}
为了从 demo2
class 访问您的 demo12
内部 class,demo2
class 必须扩展 demo1
.此外,内部classes与class的实例相连,因此您不能直接从静态方法中调用它。
Static Nested Classes
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.
Inner Classes
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
已编辑:
我建议检查一下您的设计是否 class 层次结构无法简化。特别关注内部嵌套和静态嵌套 classes.
之间的区别你的内部 class demo12
也被标记为受保护,你也可以扩展它。检查这个技巧:
package p2;
import p1.demo1;
public class demo2 extends demo1 {
protected int foo( int a, int b ) {
return (new demo22()).fun2(a, b);
}
protected class demo22 extends demo12 {
protected int fun2( int a, int b ) {
return super.fun2(a, b);
}
}
public static void main( String[] args ) {
(new demo2()).foo(2, 3);
}
}