在父类成员变量被私有化的情况下,子类继承父类后建立对象,为什么子类对象可以访问到被私有化的父类成员变量?
且在堆内存中并无父类成员变量存在,也无在内存空间中开辟内存空间?求各位大牛解答下!!
abstract class Animal {
private String name;
private String color;
public Animal() {}
public Animal(String name,String color) {
this.name=name;
this.color=color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public abstract void show();
}
class Cat extends Animal {
public Cat() {}
public Cat(String name,String color) {
super(name,color);
}
public void show() {
System.out.println("我的名字叫"+super.getName());
}
}
class Dog extends Animal {
public Dog() {}
public Dog(String name,String color) {
super(name,color);
}
public void show() {
System.out.println("我的名字叫"+super.getName());
}
}
class DuoTaiTest {
public static void main(String[] args) {
Cat c=new Cat("小黑","黑色");
System.out.println(c.getName()+"-------"+c.getColor());
c.show();
Dog d=new Dog("小白","白色");
System.out.println(d.getName()+"-------"+d.getColor());
d.show();
}
}
|
|