7.2 多态
7.2.1 引入
多态是继封装、继承之后,面向对象的第三大特性。
生活中,比如求面积的功能,圆、矩形、三角形实现起来是不一样的。跑的动作,小猫、小狗和大象,跑起来是不一样的。再比如飞的动作,昆虫、鸟类和飞机,飞起来也是不一样的。可见,同一行为,通过不同的事物,可以体现出来的不同的形态。多态,描述的就是这样的状态。
7.2.2 定义
- 多态: 是指同一行为,具有多个不同表现形式。
7.2.3 前提【重点】
- 继承父类或者实现接口【二选一】
- 方法的重写【意义体现:不重写,无意义】
- 父类引用指向子类对象【格式体现】
7.2.4 多态的体现
多态体现的格式:
父类类型 变量名 = new 子类对象;
变量名.方法名();//这个方法是父类中声明,子类中重写的方法
父类类型:指子类对象继承的父类类型,或者实现的父接口类型。
多态体现出来的现象:
编译时,看“父类”,只能调用父类声明的方法,不能调用子类扩展的方法;
运行时,看“子类”,一定是执行子类重写的方法体;
代码如下:
定义父类:
public abstract class Animal {
public abstract void eat();
}
定义子类:
class Cat extends Animal {
public void eat() {
System.out.println("吃鱼");
}
public void catchMouse(){
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("吃骨头");
}
}
定义测试类:
public class Test {
public static void main(String[] args) {
// 多态形式,创建对象
Animal a1 = new Cat();
// 调用的是 Cat 的 eat
a1.eat();
//a1.catchMouse();//错误,catchMouse()是子类扩展的方法,父类中没有
/*
多态引用,编译时,看“父类”,只能调用父类声明的方法;
运行时,看“子类”,一定是执行子类重写的方法体;
*/
// 多态形式,创建对象
Animal a2 = new Dog();
// 调用的是 Dog 的 eat
a2.eat();
}
}
7.2.5 多态的好处
1、多态参数
实际开发的过程中,父类类型作为方法形式参数,传递子类对象给方法,进行方法的调用,更能体现出多态的扩展性与便利。代码如下:
定义父类:
public abstract class Animal {
public abstract void eat();
}
定义子类:
class Cat extends Animal {
public void eat() {
System.out.println("吃鱼");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("吃骨头");
}
}
定义测试类:
public class Test {
public static void main(String[] args) {
// 多态形式,创建对象
Cat c = new Cat();
Dog d = new Dog();
// 调用showCatEat
showCatEat(c);
// 调用showDogEat
showDogEat(d);
/*
以上两个方法, 均可以被showAnimalEat(Animal a)方法所替代
而执行效果一致
*/
showAnimalEat(c);
showAnimalEat(d);
}
public static void showCatEat (Cat c){
c.eat();
}
public static void showDogEat (Dog d){
d.eat();
}
public static void showAnimalEat (Animal a){
a.eat();
}
}
由于多态特性的支持,showAnimalEat方法的Animal类型,是Cat和Dog的父类类型,父类类型接收子类对象,当然可以把Cat对象和Dog对象,传递给方法。
当eat方法执行时,多态规定,执行的是子类重写的方法,那么效果自然与showCatEat、showDogEat方法一致,所以showAnimalEat完全可以替代以上两方法。
不仅仅是替代,在扩展性方面,无论之后再多的子类出现,我们都不需要编写showXxxEat方法了,直接使用showAnimalEat都可以完成。
所以,多态的好处,体现在,可以使程序编写的更简单,并有良好的扩展。
2、多态数组
例如:家里养了两只猫,两条狗,想要统一管理他们的对象,可以使用多态数组
public class TestAnimal {
public static void main(String[] args) {
Animal[] all = new Animal[4];//可以存储各种Animal子类的对象
all[0] = new Cat();
all[1] = new Cat();
all[2] = new Dog();
all[3] = new Dog();
for (int i = 0; i < all.length; i++) {
all[i].eat();//all[i]编译时是Animal类型,运行时看存储的是什么对象
}
}
}
7.2.6 多态练习
练习1:
(1)声明抽象父类Traffic,包含抽象方法public abstract void drive()
(2)声明子类Car,Bicycle等,并重写drive方法
(3)在测试类的main中创建一个数组,有各种交通工具,遍历调用drive()方法
模拟马路上跑的各种交通工具
public abstract class Traffic {
public abstract void drive();
}
public class Car extends Traffic {
@Override
public void drive() {
System.out.println("滴滴滴...");
}
}
public class Bicycle extends Traffic {
@Override
public void drive() {
System.out.println("蹬蹬蹬。。。");
}
}
public class TestExer1 {
public static void main(String[] args) {
//右边这些是用匿名对象,初始化数组
Traffic[] arr = {new Car(),new Bicycle(),new Car(),new Bicycle()};
for (int i = 0; i < arr.length; i++) {
arr[i].drive();
}
}
}
练习2:
(1)声明一个抽象父类Person类,public abstract void toilet();
(2)声明一个子类Woman类,重写方法
(3)声明一个子类Man类,重写方法
(4)在测试类中声明一个方法,
public static void goToToilet(Person p){
p.toilet();
}
在main中,创建不同子类对象,调用goToToilet方法进行测试
public abstract class Person {
public abstract void toilet();
}
public class Man extends Person {
@Override
public void toilet() {
System.out.println("站着..");
}
}
public class Woman extends Person {
@Override
public void toilet() {
System.out.println("坐着..");
}
}
public class TestPerson {
public static void main(String[] args) {
goToToilet(new Woman());//隐含了Person p = new Woman();
goToToilet(new Man());//隐含了Person p = new Man();
}
public static void goToToilet(Person p){
p.toilet();
}
}
练习3:
1、声明一个父类Employee员工类型,有属性,姓名(String)
有方法,public abstract double earning() 用于返回实发工资
public String getInfo():显示姓名和实发工资
2、声明一个子类SalaryEmployee正式工,继承父类Employee,增加属性,薪资,工作日天数,请假天数
重写方法,public double earning()返回实发工资,实发工资 = 薪资 - 薪资/工作日天数 * 请假天数,
3、声明一个子类HourEmployee小时工,继承父类Employee
有属性,工作小时数,每小时多少钱
重写方法,public double earning()返回实发工资, 实发工资 = 每小时多少钱 * 小时数
4、声明一个子类Manager经理,继承SalaryEmployee,增加属性:奖金比例
重写方法,public double earning()返回实发工资,实发工资 = (薪资 - 薪资/工作日天数 * 请假天数)*(1+奖金比例)
5、你现在是财务,需要查看每个人的实发工资,并查看工资总额。
声明一个员工数组,存储各种员工,并遍历显示他们的姓名和实发工资,并计算工资总额
public abstract class Employee {
private String name;
public Employee(String name) {
super();
this.name = name;
}
public Employee() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract double earning();
public String getInfo() {
return "姓名:" + name + ",实发工资:" + earning();
}
}
public class SalaryEmployee extends Employee {
private double salary;
private int workingDays;//工作日天数,
private double offDays;//请假天数
public SalaryEmployee() {
super();
}
public SalaryEmployee(String name, double salary, int workingDays, double offDays) {
super(name);
this.salary = salary;
this.workingDays = workingDays;
this.offDays = offDays;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getWorkingDays() {
return workingDays;
}
public void setWorkingDays(int workingDays) {
this.workingDays = workingDays;
}
public double getOffDays() {
return offDays;
}
public void setOffDays(double offDays) {
this.offDays = offDays;
}
/*
* 重写方法,public double earning()返回实发工资,
实发工资 = 薪资 - 薪资/工作日天数 * 请假天数
*/
@Override
public double earning() {
return salary - salary/workingDays * offDays;
}
}
public class HourEmployee extends Employee {
private double moneyPerHour;
private double hours;
public HourEmployee() {
super();
}
public HourEmployee(String name, double moneyPerHour, double hours) {
super(name);
this.moneyPerHour = moneyPerHour;
this.hours = hours;
}
public double getMoneyPerHour() {
return moneyPerHour;
}
public void setMoneyPerHour(double moneyPerHour) {
this.moneyPerHour = moneyPerHour;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
/*
* 重写方法,public double earning()返回实发工资,
实发工资 = 每小时多少钱 * 小时数
*/
@Override
public double earning() {
return moneyPerHour * hours;
}
}
public class Manager extends SalaryEmployee {
private double commisionPer;
public Manager() {
super();
}
public Manager(String name, double salary, int workingDays, double offDays, double commisionPer) {
super(name, salary, workingDays, offDays);
this.commisionPer = commisionPer;
}
public double getCommisionPer() {
return commisionPer;
}
public void setCommisionPer(double commisionPer) {
this.commisionPer = commisionPer;
}
@Override
public double earning() {
return super.earning() * (1+commisionPer);
}
}
public class TestEmployee {
public static void main(String[] args) {
Employee[] all = new Employee[3];
all[0] = new HourEmployee("张三", 50, 50);
all[1] = new SalaryEmployee("李四", 10000, 22, 1);
all[2] = new Manager("老王", 20000, 22, 0, 0.3);
double sum = 0;
for (int i = 0; i < all.length; i++) {
System.out.println(all[i].getInfo());
sum += all[i].earning();
}
System.out.println("总额:" + sum);
}
}
7.2.7 父子类之间的类型转换
多态的转型分为向上转型与向下转型两种:
向上转型
- 向上转型:多态本身是子类类型向父类类型向上转换的过程,这个过程是默认的。
当父类引用指向一个子类对象时,便是向上转型。
使用格式:
父类类型 变量名 = new 子类类型();
如:Animal a = new Cat();
向下转型
- 向下转型:父类类型向子类类型向下转换的过程,这个过程是强制的。
一个已经向上转型的子类对象,将父类引用转为子类引用,可以使用强制类型转换的格式,便是向下转型。
使用格式:
子类类型 变量名 = (子类类型) 父类变量名;
如:Cat c =(Cat) a;
为什么要转型
当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则编译错误。也就是说,不能调用子类拥有,而父类没有的方法。编译都错误,更别说运行了。这也是多态给我们带来的一点"小麻烦"。所以,想要调用子类特有的方法,必须做向下转型。
转型演示,代码如下:
定义类:
abstract class Animal {
abstract void eat();
}
class Cat extends Animal {
public void eat() {
System.out.println("吃鱼");
}
public void catchMouse() {
System.out.println("抓老鼠");
}
}
class Dog extends Animal {
public void eat() {
System.out.println("吃骨头");
}
public void watchHouse() {
System.out.println("看家");
}
}
定义测试类:
public class Test {
public static void main(String[] args) {
// 向上转型
Animal a = new Cat();
a.eat(); // 调用的是 Cat 的 eat
// 向下转型
Cat c = (Cat)a;
c.catchMouse(); // 调用的是 Cat 的 catchMouse
}
}
转型的异常
转型的过程中,一不小心就会遇到这样的问题,请看如下代码:
public class Test {
public static void main(String[] args) {
// 向上转型
Animal a = new Cat();
a.eat(); // 调用的是 Cat 的 eat
// 向下转型
Dog d = (Dog)a;
d.watchHouse(); // 调用的是 Dog 的 watchHouse 【运行报错】
}
}
这段代码可以通过编译,但是运行时,却报出了 ClassCastException
,类型转换异常!这是因为,明明创建了Cat类型对象,运行时,当然不能转换成Dog对象的。这两个类型并没有任何继承关系,不符合类型转换的定义。
instanceof运算符
为了避免ClassCastException的发生,Java提供了 instanceof
关键字,给引用变量做类型的校验,只要用instanceof判断返回true的,那么强转为该类型就一定是安全的,不会报ClassCastException异常。
格式如下:
变量名/对象 instanceof 数据类型
如果变量/对象属于该数据类型,返回true。
如果变量/对象不属于该数据类型,返回false。
所以,转换前,我们最好先做一个判断,代码如下:
public class Test {
public static void main(String[] args) {
// 向上转型
Animal a = new Cat();
a.eat(); // 调用的是 Cat 的 eat
// 向下转型
if (a instanceof Cat){
Cat c = (Cat)a;
c.catchMouse(); // 调用的是 Cat 的 catchMouse
} else if (a instanceof Dog){
Dog d = (Dog)a;
d.watchHouse(); // 调用的是 Dog 的 watchHouse
}
}
}
哪些情况下instanceof判断返回true
示例代码:
class Person{
//方法代码省略...
}
class Woman extends Person{
//方法代码省略...
}
class ChineseWoman extends Woman{
//方法代码省略...
}
public class Test{
public static void main(String[] args){
Person p1 = new Person();
Person p2 = new Woman();
Person p3 = new ChineseWoman();
if(p1 instanceof Woman){//false
}
if(p2 instanceof Woman){//true
//p2转为Woman类型安全
}
if(p3 instanceof Woman){//true
//p3转为Woman类型安全
}
}
}
练习
1、声明一个父类Employee员工类型,
有属性,姓名(String),出生日期(MyDate类型,也是自定义的含年,月,日属性日期类型)
有方法,public abstract double earning()
public String getInfo():显示姓名和实发工资
2、声明一个子类SalaryEmployee正式工,继承父类Employee
增加属性,薪资,工作日天数,请假天数
重写方法,public double earning()返回实发工资, 实发工资 = 薪资 - 薪资/工作日天数 * 请假天数,
重写方法,public String getInfo():显示姓名和实发工资,月薪,工作日天数,请假天数
3、声明一个子类HourEmployee小时工,继承父类Employee
有属性,工作小时数,每小时多少钱
重写方法,public double earning()返回实发工资, 实发工资 = 每小时多少钱 * 小时数
重写方法,public String getInfo():显示姓名和实发工资,时薪,工作小时数
增加方法,public void leave():打印查看使用工具是否损坏,需要赔偿
4、声明一个子类Manager经理,继承SalaryEmployee
增加属性:奖金,奖金比例
重写方法,public double earning()返回实发工资, 实发工资 = (薪资 - 薪资/工作日天数 * 请假天数)*(1+奖金比例)
重写方法,public String getInfo():显示姓名和实发工资,月薪,工作日天数,请假天数,奖金比例
5、声明一个员工数组,存储各种员工,
你现在是人事,从键盘输入当前的月份,需要查看每个人的详细信息。
如果他是正式工(包括SalaryEmployee和Manager),并且是本月生日的,祝福生日快乐,通知领取生日礼物。如果是HourEmployee显示小时工,就进行完工检查,即调用leave方法
public abstract class Employee {
private String name;
private MyDate birthday;
public Employee(String name, MyDate birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public Employee(String name, int year, int month, int day) {
super();
this.name = name;
this.birthday = new MyDate(year, month, day);
}
public Employee() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public abstract double earning();
public String getInfo(){
return "姓名:" + name + ",生日:" + birthday.getInfo() +",实发工资:" + earning();
}
}
public class SalaryEmployee extends Employee {
private double salary;
private int workingDays;//工作日天数,
private double offDays;//请假天数
public SalaryEmployee() {
super();
}
public SalaryEmployee(String name, int year, int month, int day, double salary, int workingDays, double offDays) {
super(name, year, month, day);
this.salary = salary;
this.workingDays = workingDays;
this.offDays = offDays;
}
public SalaryEmployee(String name, MyDate birthday, double salary, int workingDays, double offDays) {
super(name, birthday);
this.salary = salary;
this.workingDays = workingDays;
this.offDays = offDays;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getWorkingDays() {
return workingDays;
}
public void setWorkingDays(int workingDays) {
this.workingDays = workingDays;
}
public double getOffDays() {
return offDays;
}
public void setOffDays(double offDays) {
this.offDays = offDays;
}
/*
* 重写方法,public double earning()返回实发工资,
实发工资 = 薪资 - 薪资/工作日天数 * 请假天数
*/
@Override
public double earning() {
return salary - salary/workingDays * offDays;
}
@Override
public String getInfo() {
return super.getInfo() + ",月薪:" + salary + ",工作日:" + workingDays +",请假天数:" + offDays;
}
}
public class HourEmployee extends Employee {
private double moneyPerHour;
private double hours;
public HourEmployee() {
super();
}
public HourEmployee(String name, int year, int month, int day, double moneyPerHour, double hours) {
super(name, year, month, day);
this.moneyPerHour = moneyPerHour;
this.hours = hours;
}
public HourEmployee(String name, MyDate birthday, double moneyPerHour, double hours) {
super(name, birthday);
this.moneyPerHour = moneyPerHour;
this.hours = hours;
}
public double getMoneyPerHour() {
return moneyPerHour;
}
public void setMoneyPerHour(double moneyPerHour) {
this.moneyPerHour = moneyPerHour;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
/*
* 重写方法,public double earning()返回实发工资,
实发工资 = 每小时多少钱 * 小时数
*/
@Override
public double earning() {
return moneyPerHour * hours;
}
@Override
public String getInfo() {
return super.getInfo() + ",时薪:" + moneyPerHour + ",小时数:" + hours;
}
public void leave(){
System.out.println("小时工,查看使用工具是否损坏,需要赔偿,然后拿钱走人");
}
}
public class Manager extends SalaryEmployee {
private double commisionPer;
public Manager() {
super();
}
public Manager(String name, int year, int month, int day, double salary, int workingDays, double offDays,
double commisionPer) {
super(name, year, month, day, salary, workingDays, offDays);
this.commisionPer = commisionPer;
}
public Manager(String name, MyDate birthday, double salary, int workingDays, double offDays, double commisionPer) {
super(name, birthday, salary, workingDays, offDays);
this.commisionPer = commisionPer;
}
public double getCommisionPer() {
return commisionPer;
}
public void setCommisionPer(double commisionPer) {
this.commisionPer = commisionPer;
}
@Override
public double earning() {
return super.earning() * (1+commisionPer);
}
@Override
public String getInfo() {
return super.getInfo() + ",奖金比例:" + commisionPer;
}
}
public class TestEmployee {
public static void main(String[] args) {
Employee[] all = new Employee[3];
/*all[0] = new HourEmployee("张三", new MyDate(1990, 5, 1), 50, 50);
all[1] = new SalaryEmployee("李四", new MyDate(1991, 1, 1), 10000, 22, 1);
all[2] = new Manager("老王", new MyDate(1987, 12, 8), 20000, 22, 0, 0.3);*/
all[0] = new HourEmployee("张三", 1990, 5, 1, 50, 50);
all[1] = new SalaryEmployee("李四", 1991, 1, 1, 10000, 22, 1);
all[2] = new Manager("老王", 1987, 12, 8, 20000, 22, 0, 0.3);
//从键盘输入当前的月份
Scanner input = new Scanner(System.in);
System.out.print("请输入当前月份:");
int month;
while(true){
month = input.nextInt();
if(month>=1 && month<=12){
break;
}
}
input.close();
for (int i = 0; i < all.length; i++) {
System.out.println(all[i].getInfo());
if(all[i] instanceof SalaryEmployee){
if(month == all[i].getBirthday().getMonth()){
System.out.println(all[i].getName() +"生日快乐,领取生日补助购物卡");
}
}else{
HourEmployee he = (HourEmployee) all[i];
he.leave();
}
}
}
}
7.2.8 属性、静态方法没有多态性
1、属性没有多态性
如果直接访问成员变量,那么只看编译时类型
public class TestField {
public static void main(String[] args) {
Father f = new Son();
System.out.println(f.x);//只看编译时类型
}
}
class Father{
int x = 1;
}
class Son extends Father{
int x = 2;
}
2、静态方法没有多态性
public class TestField {
public static void main(String[] args) {
Father f = new Son();
f.test();//只看编译时类型
}
}
class Father{
public static void test(){
System.out.println("father");
}
}
class Son extends Father{
public static void test(){
System.out.println("son");
}
}
小贴士:
静态方法不能被重写
调用静态方法最好使用“类名.”