多态&抽象类&接口
# 1. 多态
# 1.1 多态的概述(记忆)
什么是多态
同一个对象,在不同时刻表现出来的不同形态
多态的前提
- 要有继承或实现关系
- 要有方法的重写(方法重写的快捷键:输入要重写的方法名)
- 要有父类引用指向子类对象
# 1.2 多态中的成员访问特点(记忆)
成员访问特点
成员变量
编译看父类/左边,运行看父类/右边
成员方法
编译看父类/左边,运行看子类/右边
为什么成员变量和成员方法的访问不一样呢?
因为成员方法有重写,而成员变量没有
代码演示
动物类
public class Animal { public int age = 40; public void eat() { System.out.println("动物吃东西"); } }
1
2
3
4
5
6
7猫类
//有继承 public class Cat extends Animal { public int age = 20; public int weight = 10; //有方法重写 @Override public void eat() { System.out.println("猫吃鱼"); } public void playGame() { System.out.println("猫捉迷藏"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14测试类
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(); } }
1
2
3
4
5
6
7
8
9
10
11
# 1.3 多态的好处和弊端(记忆)
好处
提高程序的扩展性。定义方法时候,使用父类型作为参数,在使用的时候,使用具体的子类型参与操作
弊端
不能使用子类的特有成员
# 1.4 多态中的转型(应用)
向上转型
父类引用指向子类对象就是向上转型
向下转型
格式:子类型 对象名 = (子类型)父类引用;
代码演示
动物类
public class Animal { public void eat() { System.out.println("动物吃东西"); } }
1
2
3
4
5猫类
public class Cat extends Animal { @Override public void eat() { System.out.println("猫吃鱼"); } public void playGame() { System.out.println("猫捉迷藏"); } }
1
2
3
4
5
6
7
8
9
10测试类
public class AnimalDemo { public static void main(String[] args) { //多态 //向上转型 Animal a = new Cat(); a.eat(); // a.playGame(); //向下转型 Cat c = (Cat)a; c.eat(); c.playGame(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1.5 多态的案例(应用)
案例需求
请采用多态的思想实现猫和狗的案例,并在测试类中进行测试
代码实现
动物类
public class Animal { private String name; private int age; public Animal() { } public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void eat() { System.out.println("动物吃东西"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32猫类
public class Cat extends Animal { public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("猫吃鱼"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14狗类
public class Dog extends Animal { public Dog() { } public Dog(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("狗吃骨头"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14测试类
public class AnimalDemo { public static void main(String[] args) { //创建猫类对象进行测试 Animal a = new Cat(); a.setName("加菲"); a.setAge(5); System.out.println(a.getName() + "," + a.getAge()); a.eat(); a = new Cat("加菲", 5); System.out.println(a.getName() + "," + a.getAge()); a.eat(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2. 抽象类
# 2.1 抽象类的概述(理解)
父类中的方法,如果被它的子类们都重写了,那么父类的方法就只有声明格式还有意义,而方法体则没有存在的意义了(因为子类对象会调用自己的方法重写)。
换句话说,父类知道子类应该实现什么功能,但是不知道功能具体怎么实现(由子类自己决定)。对于这样的方法,父类完全只需要提供一个方法声明即可,具体实现交给子类自己去实现。
对于这种只有方法声明,没有方法实现的方法,称为抽象方法。而拥有抽象方法的类,则必须定义为抽象类。
# 2.2 抽象类的特点(记忆)
抽象类和抽象方法必须使用 abstract 关键字修饰
//抽象类的定义 public abstract class 类名 {} //抽象方法的定义 public abstract void eat();
1
2
3
4
5抽象类中不一定有抽象方法,有抽象方法的类一定是抽象类
抽象类不能实例化
抽象类如何实例化呢?参照多态的方式,通过子类对象实例化,这叫抽象类多态
抽象类的子类
- 要么重写抽象类中的所有抽象方法
- 要么是抽象类
思考:抽象方法能否被private、static、final这些关键字修饰?
- 不能,被private、static、final修饰的方法都不能被重写,而抽象方法存在的目的就是要被子类重写。
# 2.3 抽象类的成员特点(记忆)
成员的特点
- 成员变量:既可以是变量,也可以是常量
- 构造方法
- 空参构造
- 有参构造
- 成员方法
- 抽象方法
- 普通方法
代码演示
动物类
public abstract class Animal { private int age = 20; private final String city = "北京"; public Animal() {} public Animal(int age) { this.age = age; } public void show() { age = 40; System.out.println(age); // city = "上海"; System.out.println(city); } public abstract void eat(); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20猫类
public class Cat extends Animal { @Override public void eat() { System.out.println("猫吃鱼"); } }
1
2
3
4
5
6测试类
public class AnimalDemo { public static void main(String[] args) { Animal a = new Cat(); a.eat(); a.show(); } }
1
2
3
4
5
6
7
# 2.4 抽象类的案例(应用)
案例需求
请采用抽象类的思想实现猫和狗的案例,并在测试类中进行测试
代码实现
动物类
public abstract class Animal { private String name; private int age; public Animal() { } public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public abstract void eat(); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30猫类
public class Cat extends Animal { public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("猫吃鱼"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14狗类
public class Dog extends Animal { public Dog() { } public Dog(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("狗吃骨头"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14测试类
public class AnimalDemo { public static void main(String[] args) { //创建对象,按照多态的方式 Animal a = new Cat(); a.setName("加菲"); a.setAge(5); System.out.println(a.getName()+","+a.getAge()); a.eat(); System.out.println(""); a = new Cat("加菲",5); System.out.println(a.getName()+","+a.getAge()); a.eat(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 3. 设计模式_模板方法模式
# 3.1 设计模式的概念
- 设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。
- 使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性、重用性。
# 3.2 模板方法模式
- 规定一个操作的基本流程,而将某些实现细节定义成抽象方法。
- 子类通过重写抽象方法,并调用从父类继承而来的模板方法即可。
# 3.3 模板方法优点
- 规范了不变的流程部分,扩展了可变的细节部分。
- 提取公共代码,利于后期维护。
# 3.4 模板方法模式案例1-医生看病
# 3.4.1 需求
通常来说,医生看病的流程都是固定的:登记,检查病因、开单、抓药。对传统中医来说,检查病因的方式通常是“望闻问切”了解病况,而西医则是根据化验报告了解病因。
因为总体的看病流程是相同的,此时可以定义一个方法控制流程,而检查病因的步骤可以定义成抽象方法,由子类实现。这就是模板方法的体现。
# 3.4.2 代码实现
Doctor类
//模板:模板看病的流程 public abstract class Doctor { //定义模板方法:规定看病的流程,流程是不允许被修改的,final public final void work(){ System.out.println("登记"); //System.out.println("检查病因"); //由于检查病因,不同的医生(子类)有不同的实现效果,定义成抽象方法 cheackBody(); System.out.println("开单"); System.out.println("抓药"); } public abstract void cheackBody(); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17ChineseDoctor
public class ChineseDoctor extends Doctor{ @Override public void cheackBody() { System.out.println("中医望闻问切,检查病因"); } }
1
2
3
4
5
6WesternDoctor
public class WesternDoctor extends Doctor{ @Override public void cheackBody() { System.out.println("西医抽血化验,检查病因"); } }
1
2
3
4
5
6demo1
public class Demo1 { public static void main(String[] args) { ChineseDoctor c = new ChineseDoctor(); c.work(); System.out.println("-----------------"); WesternDoctor w = new WesternDoctor(); w.work(); } }
1
2
3
4
5
6
7
8
9
10
11
# 3.5 模板方法改写加油站收银系统
代码实现
Demo1
/* 分析: 创建一个抽象卡类(Card),提供卡号(id)、余额(balance)、抽象的支付方法(pay) 创建一个金卡类(GoldenCard):重写支付功能,按照原价的8折计算输出 创建一个银卡类(SilverCard):重写支付功能,按照原价的8.5折计算输出 */ import java.util.Scanner; public class Demo1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入加油的金额:"); double money = sc.nextDouble(); GoldenCard g = new GoldenCard("vip001", 10000); g.pay(money); System.out.println("----------------"); System.out.println("请输入加油的金额:"); double money1 = sc.nextDouble(); SilverCard s = new SilverCard("vip002", 10000); s.pay(money); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28Card
//创建一个抽象卡类(Card),提供卡号(id)、余额(balance)、抽象的支付方法(pay) public abstract class Card { private String id; private double balance; //模板方法:规定整个支付的流程 public final void pay(double money) { System.out.println("支付成功"); System.out.println("原价:" + money); double price = discount(money); System.out.println("折扣价:" + price); double newBalance = getBalance() - price; System.out.println("余额:" + newBalance); //将新的余额更新 setBalance(newBalance); } //具体的打折放啊,不同的 子类实现方式不同,所以定义成抽象方法,让子类去重写 public abstract double discount(double oldMoney); public Card() { } public Card(String id, double balance) { this.id = id; this.balance = balance; } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46GoldenCard
//创建一张金卡类(GoldenCard):重写支付功能,按照原价的8折计算输出 public class GoldenCard extends Card { //重写支付算法 @Override public double discount(double oldMoney) { return oldMoney*0.8; } public GoldenCard() { } public GoldenCard(String id, double balance) { super(id, balance); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16SilverCard
//创建一张银卡类(SilverCard):重写支付功能,按照原价的85折计算输出 public class SilverCard extends Card { //重写支付算法 @Override public double discount(double oldMoney) { return oldMoney*0.85; } public SilverCard() { } public SilverCard(String id, double balance) { super(id, balance); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 4. 接口
# 4.1 接口的概述(理解)
接口就是一种公共的规范标准,只要符合规范标准,大家都可以通用。
Java中的接口主要是对功能的描述和规范,更多的体现在对行为的抽象!
接口的意义:
- 接口多用于约束和统一功能,一个接口体现的是能做什么,而不关心怎么做
- 接口可以提高程序的拓展性和维护性。
# 4.2 接口的特点(记忆)
接口用关键字interface修饰
权限修饰符 interface 接口名 {} // 接口的权限修饰符只能是public或缺省(不写)
1
2类实现接口用implements表示
public class 类名 implements 接口名 {}
1接口不能实例化
- 接口如何实例化呢?参照多态的方式,通过实现类对象实例化,这叫接口多态。
- 多态的形式:具体类多态,抽象类多态,接口多态。
接口的子类
- 要么重写接口中的所有抽象方法
- 要么子类也是抽象类
# 4.3 接口的成员特点(记忆)
成员特点
成员变量
只能是常量
默认修饰符:public static final
构造方法
没有,因为接口主要是扩展功能的,而没有具体存在,所以也不能创建对象。
成员方法
只能是抽象方法
默认修饰符:public abstract
关于接口中的方法,JDK8和JDK9中有一些新特性,后面再讲解
代码演示
接口
public interface Inter { public int num = 10; public final int num2 = 20; // public static final int num3 = 30; int num3 = 30; // public Inter() {} // public void show() {} public abstract void method(); void show(); }
1
2
3
4
5
6
7
8
9
10
11
12
13实现类
public class InterImpl extends Object implements Inter { public InterImpl() { super(); } @Override public void method() { System.out.println("method"); } @Override public void show() { System.out.println("show"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15测试类
public class InterfaceDemo { public static void main(String[] args) { Inter i = new InterImpl(); // i.num = 20; System.out.println(i.num); // i.num2 = 40; System.out.println(i.num2); System.out.println(Inter.num); } }
1
2
3
4
5
6
7
8
9
10
# 4.4 接口的案例(应用)
案例需求
对猫和狗进行训练,他们就可以跳高了,这里加入跳高功能。
请采用抽象类和接口来实现猫狗案例,并在测试类中进行测试。
代码实现
动物类
public abstract class Animal { private String name; private int age; public Animal() { } public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public abstract void eat(); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30跳过接口
public interface Jumpping { public abstract void jump(); }
1
2
3猫类
public class Cat extends Animal implements Jumpping { public Cat() { } public Cat(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("猫吃鱼"); } @Override public void jump() { System.out.println("猫可以跳高了"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19测试类
public class AnimalDemo { public static void main(String[] args) { //创建对象,调用方法 Jumpping j = new Cat(); j.jump(); System.out.println(""); Animal a = new Cat(); a.setName("加菲"); a.setAge(5); System.out.println(a.getName()+","+a.getAge()); a.eat(); // a.jump(); a = new Cat("加菲",5); System.out.println(a.getName()+","+a.getAge()); a.eat(); System.out.println(""); Cat c = new Cat(); c.setName("加菲"); c.setAge(5); System.out.println(c.getName()+","+c.getAge()); c.eat(); c.jump(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 4.5 类和接口的关系(记忆)
类与类的关系
继承关系,只能单继承,但是可以多层继承
类与接口的关系
实现关系,可以单实现,也可以多实现,还可以在继承一个类的同时实现多个接口
接口与接口的关系
继承关系,可以单继承,也可以多继承
# 4.6 抽象类和接口的区别(记忆)
成员区别
抽象类
变量,常量;有构造方法;有抽象方法,也有非抽象方法
接口
常量;抽象方法
关系区别
类与类
继承,单继承
类与接口
实现,可以单实现,也可以多实现
接口与接口
继承,单继承,多继承
设计理念区别
抽象类
对类抽象,包括属性、行为
接口
对行为抽象,主要是行为
# 5. 综合案例
# 5.1 案例需求(理解)
我们现在有乒乓球运动员和篮球运动员,乒乓球教练和篮球教练。
为了出国交流,跟乒乓球相关的人员都需要学习英语。
请用所学知识分析,这个案例中有哪些具体类,哪些抽象类,哪些接口,并用代码实现。
# 5.2 代码实现
抽象类-人:
public abstract class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
抽象类-运动员:
public abstract class Player extends Person {
public Player() {
}
public Player(String name, int age) {
super(name, age);
}
public abstract void study();
}
2
3
4
5
6
7
8
9
10
抽象教练类:
public abstract class Coach extends Person {
public Coach() {
}
public Coach(String name, int age) {
super(name, age);
}
public abstract void teach();
}
2
3
4
5
6
7
8
9
10
学英语接口:
public interface SpeakEnglish {
public abstract void speak();
}
2
3
篮球教练:
public class BasketballCoach extends Coach {
public BasketballCoach() {
}
public BasketballCoach(String name, int age) {
super(name, age);
}
@Override
public void teach() {
System.out.println("篮球教练教如何运球和投篮");
}
@Override
public void eat() {
System.out.println("篮球教练吃羊肉,喝羊奶");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
乒乓球教练:
public class PingPangCoach extends Coach implements SpeakEnglish {
public PingPangCoach() {
}
public PingPangCoach(String name, int age) {
super(name, age);
}
@Override
public void teach() {
System.out.println("乒乓球教练教如何发球和接球");
}
@Override
public void eat() {
System.out.println("乒乓球教练吃小白菜,喝大米粥");
}
@Override
public void speak() {
System.out.println("乒乓球教练说英语");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
乒乓球运动员:
public class PingPangPlayer extends Player implements SpeakEnglish {
public PingPangPlayer() {
}
public PingPangPlayer(String name, int age) {
super(name, age);
}
@Override
public void study() {
System.out.println("乒乓球运动员学习如何发球和接球");
}
@Override
public void eat() {
System.out.println("乒乓球运动员吃大白菜,喝小米粥");
}
@Override
public void speak() {
System.out.println("乒乓球运动员说英语");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
篮球运动员:
public class BasketballPlayer extends Player {
public BasketballPlayer() {
}
public BasketballPlayer(String name, int age) {
super(name, age);
}
@Override
public void study() {
System.out.println("篮球运动员学习如何运球和投篮");
}
@Override
public void eat() {
System.out.println("篮球运动员吃牛肉,喝牛奶");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19