Parent p = new Child();
多态成员访问的特点
成员变量
编译看左边(基类),运行看左边(基类);无论如何都是访问基类的成员变量。
成员方法
编译看左边(基类),运行看右边(派生类),动态绑定。
Static方法
编译看左边(基类),运行看左边(基类)。
只有非静态的成员方法,编译看左边,运行看右边。
这样,我们也可以得出多态的局限:
不能使用派生类特有的成员属性和派生类特有的成员方法。
代码举例:
- 创建父类Animal类
public class Animal {
int age = 1;
static int rank = 11;
String name;
public Animal(){
this.name = "Animal";
}
public String getName(){
return this.name;
}
public int getAge(){
return age;
}
public int getRank(){
return this.rank;
}
public static int getStaticRank(){
return rank;
}
}
- 创建子类Cat
public class Cat extends Animal{
int age = 2;
static int rank = 22;
Cat(){
name = "Cat";
}
public int getAge(){
return age;
}
public int getRank(){
return this.rank;
}
public static int getStaticRank(){
return rank;
}
}
- 创建子类Dog
public class Dog extends Animal{
int age = 3;
static int rank = 33;
Dog(){
name = "Dog";
}
public int getAge(){
return age;
}
public int getRank(){
return this.rank;
}
public static int getStaticRank(){
return rank;
}
}
- 验证
public class Test {
public static void main(String[] args) {
Animal animal = new Animal();
Animal cat = new Cat();
Animal dog = new Dog();
System.out.println("====================================");
System.out.println("animal : "+animal.getName());
System.out.println("cat : "+cat.getName());
System.out.println("dog : "+dog.getName());
System.out.println("====================================");
System.out.println("animal age is " + animal.age);
System.out.println("cat age is " + cat.age);
System.out.println("dog age is " + dog.age);
System.out.println("====================================");
System.out.println("animal get age is " + animal.getAge());
System.out.println("cat get age is " + cat.getAge());
System.out.println("dog get age is " + dog.getAge());
System.out.println("====================================");
System.out.println("animal rank is "+animal.rank);
System.out.println("cat rank is "+cat.rank);
System.out.println("dog rank is "+dog.rank);
System.out.println("====================================");
System.out.println("animal get rank is "+animal.getRank());
System.out.println("cat get rank is "+cat.getRank());
System.out.println("dog get rank is "+dog.getRank());
System.out.println("====================================");
System.out.println("animal get static rank is "+animal.getStaticRank());
System.out.println("cat get static rank is "+cat.getStaticRank());
System.out.println("dog get static rank is "+dog.getStaticRank());
}
}
- 输出结果
====================================
animal : Animal
cat : Cat
dog : Dog
====================================
animal age is 1
cat age is 1
dog age is 1
====================================
animal get age is 1
cat get age is 2
dog get age is 3
====================================
animal rank is 11
cat rank is 11
dog rank is 11
====================================
animal get rank is 11
cat get rank is 22
dog get rank is 33
====================================
animal get static rank is 11
cat get static rank is 11
dog get static rank is 11
参考:https://www.cnblogs.com/scyq/p/11667881.html