成员变量:编译看左边,执行看左边
成员方法:编译看左边,执行看右边
为什么成员变量和成员方法的访问不一样呢?
因为成员方法有重写,而成员变量没有
package com.ithema_36;
public class Animal {
public int age = 40;
public void eat(){
System.out.println("动物吃东西");
}
}
package com.ithema_36;
/*
测试类
*/
public class AnimalDemo {
public static void main(String[] args) {
//有父类引用指向子类对象
Animal a = new Cat();
System.out.println(a.age);
// System.out.println(a.weight);
a.eat();
// a.playGame();
}
}
package com.ithema_36;
public class Cat extends Animal{
public int age = 20;
public int weight = 10;
public void eat() {
System.out.println("猫吃鱼");
}
public void playGame(){
System.out.println("猫捉迷藏");
}
}