public class TestPublicPrivate {
int a=1;
private int e =7;
public void showE(){
System.out.println(e);
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
public class TestPrivate extends TestPublicPrivate {
public void setE(int x){
this.e=x;
}
public int getE(){
return e;
}
//e报错
将鼠标放到e上会显示
‘e’ has private access in ‘TestPublicPrivate’
public class TestPrivate extends TestPublicPrivate {
int a = 5;
public void setA(int x){
this.a=3;
}
public static void main(String[] args) {
TestPrivate a1 = new TestPrivate();
System.out.println(a1.getA());
a1.setA( 10 );
System.out.println(a1.getA());
}
}
这里相当于破坏了setter方法
这里两个getA()==1;
还是隐形的调用了父类的getter的方法。
public class TestPrivate extends TestPublicPrivate {
int a = 5;
public void setA(int x){
this.a=3;
}
public int getA(){
return a;
}
public static void main(String[] args) {
TestPrivate a1 = new TestPrivate();
System.out.println(a1.getA());
a1.setA( 10 );
System.out.println(a1.getA());
}
这里第一个 a1.get()==5;
第二个a1.get()==3;
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
官方文档说明无法继承