/*
猫狗案例,加入跳高的额外功能
分析:从具体到抽象
猫:
姓名,年龄
吃饭,睡觉
狗:
姓名,年龄
吃饭,睡觉
由于有共性功能,所以,我们抽取出一个父类:
动物:
姓名,年龄
吃饭();
睡觉(){}
猫:继承自动物
狗:继承自动物
跳高的额外功能是一个新的扩展功能,所以我们要定义一个接口
接口:
跳高
部分猫:实现跳高
部分狗:实现跳高
实现;
从抽象到具体
使用:
使用具体类
*/
//定义跳高接口
interface Jumpping {
//跳高功能
public abstract void jump();
}
//定义抽象类
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();
//睡觉(){}
public void sleep() {
System.out.println("睡觉觉了");
}
}
//具体猫类
class Cat extends Animal {
public Cat(){}
public Cat(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("猫吃鱼");
}
}
//具体狗类
class Dog extends Animal {
public Dog(){}
public Dog(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("狗吃肉");
}
}
//有跳高功能的猫
class JumpCat extends Cat implements Jumpping {
public JumpCat() {}
public JumpCat(String name,int age) {
super(name,age);
}
public void jump() {
System.out.println("跳高猫");
}
}
//有跳高功能的狗
class JumpDog extends Dog implements Jumpping {
public JumpDog() {}
public JumpDog(String name,int age) {
super(name,age);
}
public void jump() {
System.out.println("跳高狗");
}
}
class InterfaceTest {
public static void main(String[] args) {
//定义跳高猫并测试
JumpCat jc = new JumpCat();
jc.setName("哆啦A梦");
jc.setAge(3);
System.out.println(jc.getName()+"---"+jc.getAge());
jc.eat();
jc.sleep();
jc.jump();
System.out.println("-----------------");
JumpCat jc2 = new JumpCat("加菲猫",2);
System.out.println(jc2.getName()+"---"+jc2.getAge());
jc2.eat();
jc2.sleep();
jc2.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
老师和学生案例,加入抽烟的额外功能
分析:从具体到抽象
老师:姓名,年龄,吃饭,睡觉
学生:姓名,年龄,吃饭,睡觉
由于有共性功能,我们提取出一个父类,人类。
人类:
姓名,年龄
吃饭();
睡觉(){}
抽烟的额外功能不是人或者老师,或者学生一开始就应该具备的,所以,我们把它定义为接口
抽烟接口。
部分老师抽烟:实现抽烟接口
部分学生抽烟:实现抽烟接口
实现:从抽象到具体
使用:具体
*/
//定义抽烟接口
interface Smoking {
//抽烟的抽象方法
public abstract void smoke();
}
//定义抽象人类
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();
//睡觉(){}
public void sleep() {
System.out.println("睡觉觉了");
}
}
//具体老师类
class Teacher extends Person {
public Teacher() {}
public Teacher(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("吃大白菜");
}
}
//具体学生类
class Student extends Person {
public Student() {}
public Student(String name,int age) {
super(name,age);
}
public void eat() {
System.out.println("吃红烧肉");
}
}
//抽烟的老师
class SmokingTeacher extends Teacher implements Smoking {
public SmokingTeacher() {}
public SmokingTeacher(String name,int age) {
super(name,age);
}
public void smoke() {
System.out.println("抽烟的老师");
}
}
//抽烟的学生
class SmokingStudent extends Student implements Smoking {
public SmokingStudent() {}
public SmokingStudent(String name,int age) {
super(name,age);
}
public void smoke() {
System.out.println("抽烟的学生");
}
}
class InterfaceTest2 {
public static void main(String[] args) {
//测试学生
SmokingStudent ss = new SmokingStudent();
ss.setName("林青霞");
ss.setAge(27);
System.out.println(ss.getName()+"---"+ss.getAge());
ss.eat();
ss.sleep();
ss.smoke();
System.out.println("-------------------");
SmokingStudent ss2 = new SmokingStudent("刘意",30);
System.out.println(ss2.getName()+"---"+ss2.getAge());
ss2.eat();
ss2.sleep();
ss2.smoke();
//测试老师留给自己练习
}
}