笔记来自2019求知讲堂零基础Java入门编程视频教程 https://www.bilibili.com/video/av76235341
多态(Polymorphism)
什么是多态
在java中,多态有两种体现
- 方法的重载(overload)和重写(overwrite)。
- 对象的多态性——子类的对象可以代替父类的对象使用。
属性没有多态。
java引用变量有两个类型,编译时类型和运行时类型。
编译时类型由声明该变量是使用的类型决定,运行时类型由实际赋给该变量的对象决定。(简单来理解就是Person p = new Person();中,等号左边是编译时类型,等号右边是运行时类型)
若编译时类型和运行时类型不一致,就出现对象的多态(Polymorphism)。
Person e = new Student();
//声明一个Person类的变量e,但它指向了一个Student类型的对象。
与基础类型变量只能有一种确定的类型不同,一个引用变量可能指向(引用)多种不同类型的对象。
Person p = new Person();
//声明一个Person类的变量p,让他指向一个Person类型对象
p = new Student();
//此时得p指向的是一个Student类型的对象
向上转型和虚拟方法调用
向上转型(upcasting)
多态所指的编译时类型和运行时类型的不一致不是随意的,必须是父类类型的引用指向子类的对象,也就是运行时类型必须是编译时类型的子类,因为子类可以看做是特殊的父类,这就是向上转型。
如果一个引用类型变量声明为父类的类型,但实际引用的是子类的对象,那么该变量就不能再访问子类中额外添加的属性和方法了。
这是因为属性是编译时确定的,以Person e = new Student();为例,编译时e为Person类型,所以只有Person类中的age、name、sex属性,而没有Student类自己声明的school属性。
虚拟方法调用(Virtual Method Invocation)
上面的情况是在子类中额外添加属性和方法,如果一个方法是子类对父类方法的重写,那么这个子类方法是可以被调用的,这就是虚拟方法调用。
public class Person {
int age = 1;
String name = "zhangsan";
int sex = 0;
public void showInfo() {
System.out.println("这是父类的showInfo()");
}
public class Student extends Person{
String school;
public void showInfo() {
System.out.println("这是子类对showInfo()的重写");
}
}
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.showInfo(); //运行结果:这是父类的showInfo()
Person e = new Student();
e.showInfo(); //运行结果:这是子类对showInfo()的重写
首先虽然编译时e为Person类型,但是方法有重写,就是说Person类和Student类都有showInfo()方法,这保证了编译时不会报错;
其次,方法的调用是在运行时确定的,所以方法实际调用的是Student类的showInf()方法,这也叫做动态绑定。
注:
成员方法多态性的前提是方法重写:
编译时,要查看引用变量所属的类中是否有所调用的方法
运行时,调用实际对象所属的类中的重写方法
而成员变量不具备多态性,只看引用变量所属的类
多态的应用
方法声明时的形参为父类类型,实参为子类类型调用方法。
假设有一个Animal类,类中有一个打印叫声的方法shout(),Dog类和Cat类是其子类,重写了shout()方法。
//Animal类,有一个打印叫声的方法shout()
public class Animal {
public void shout() {
System.out.println("叫了一声");
}
}
//Dog类,重写shout()方法
class dog extends Animal{
@Override
public void shout() {
System.out.println("汪汪汪");
}
}
//Cat类,重写shout()方法
class Cat extends Animal{
@Override
public void shout() {
System.out.println("喵喵喵");
}
}
当不存在多态,如果想要在测试类中调用这三个方法,就需要写多个重载的方法,如果需要新增一个子类老虎,也要重写shout()方法,那么就要再多写一个重载方法,很明显这样是十分繁琐的。
//不使用多态的情况
public class Test {
public static void main(String[] args) {
animalShout(new Animal());
animalShout(new Dog());
animalShout(new Cat());
}
//构成重载的三个方法
static void animalShout(Animal a) {
a.shout();
}
static void animalShout(Cat c) {
c.shout();
}
static void animalShout(Dog d) {
d.shout();
}
}
所以要应用多态,使用多态后,只需要写一个方法,用他们的父类接收参数,然后根据传入参数的类型,进行动态绑定。
//不使用多态的情况
public class Test {
public static void main(String[] args) {
animalShout(new Animal());
animalShout(new Dog());
animalShout(new Cat());
}
static void animalShout(Animal a) {
a.shout();
}
}
instanceof操作符
x instanceof A:检验x是否为类A的对象。返回值为Boolean型
注:要求x所属的类与类A必须是子类和父类的关系,否则编译错误(Incompatible conditional operand types Circle and Student)
Student stu = new Student();
Person p = new Person();
System.out.println(stu instanceof Person); //true
System.out.println(p instanceof Student); //false
Person e = new Student();
System.out.println(e instanceof Student); //true