我有一个我需要修改的java公共类(只有一个方法).这个类在一个包中,所以我正在编写一个新类,它扩展了第一个类并覆盖了我需要更改的方法.
A级是
public class A {
GL gl;
GLU glu;
PGraphicsOpenGL pgrap;
//other fields
//constructor
public void method() {
this.gl = pgrap.gl;
this.glu = pgrap.glu;
//something else I don't want in class B
}
}
Run Code Online (Sandbox Code Playgroud)
B类是这样的
public class B extends A {
//constructor that recalls super()
public void method() {
super.gl = pgrap.gl;
super.glu = pgrap.glu;
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误super.gl = pgrap.gl:The field A.gl is not visible.我没有在包中写入任何getter方法,我该怎么办?
谢谢.
注意:我无法重新编译包或将类B添加到包中.
默认访问说明符是package-private指同一个包中的类A可以使用实例访问此变量A
A a = ....
a.gl = ...; // this works.
Run Code Online (Sandbox Code Playgroud)
和package-private成员(和private会员)不能被继承,只有protected和public成员.
由于A#method()已经在做赋值操作,你可以调用super.method()在B#method()得到你想要的行为.或者你应该将它们标记为protected.