Java核心技术卷1第五章的感想和记录

【练习题】抽象类、继承、接口综合

设计一个系统:
xxx纯净水生产线
目前流程是:从某个地方把水取出来,然后经过缓冲,过滤,加热和放糖的步骤

1
2
3
4
5
6
7
8
9
10
abstract 水{
public void 水();
}
interface 过滤{}
interface 缓冲{}
interface 加热{}
interface 放糖{}
class 纯净水1 extendsimps 过滤,缓冲{}
class 纯净水2 extendsimps 缓冲{}
class 纯净水2 extendsimps 过滤{}

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
package com.a4_22;
/**
* Created by HongWeiPC on 2017/5/4.
*/
public abstract class Water {
}
interface Buffer {
String Buffer();
}
interface Filter {
String Filter();
}
interface Heating {
String Heating();
}
interface Sugaring {
String Sugaring();
}
class Water1 extends Water implements Buffer, Filter {
public String toString() {
return "纯净水1:" + this.Buffer() + " " + this.Filter();
}
@Override
public String Buffer() {
return "缓冲";
}
@Override
public String Filter() {
return "过滤";
}
}
class Water2 extends Water implements Buffer {
@Override
public String toString() {
return "纯净水2:" + this.Buffer();
}
@Override
public String Buffer() {
return "缓冲";
}
}
class Water3 extends Water implements Filter {
@Override
public String toString() {
return "纯净水3:" + this.Filter();
}
@Override
public String Filter() {
return "过滤";
}
}
class Water4 extends Water implements Buffer, Filter, Heating {
@Override
public String toString() {
return "纯净水4:" + this.Buffer() + " " + this.Filter() + " " + this.Heating();
}
@Override
public String Buffer() {
return "缓冲";
}
@Override
public String Filter() {
return "过滤";
}
@Override
public String Heating() {
return "加热";
}
}
class Water5 extends Water implements Buffer, Filter, Heating, Sugaring {
@Override
public String toString() {
return "纯净水5:" + this.Buffer() + " " + this.Filter() + " " + this.Heating() + " " + this.Sugaring();
}
@Override
public String Buffer() {
return "缓冲";
}
@Override
public String Filter() {
return "过滤";
}
@Override
public String Heating() {
return "加热";
}
@Override
public String Sugaring() {
return "放糖";
}
}
class TestWater {
public static void main(String[] args) {
Water1 water1 = new Water1();
Water2 water2 = new Water2();
Water3 water3 = new Water3();
Water4 water4 = new Water4();
Water5 water5 = new Water5();
System.out.println(water1.toString());
System.out.println(water2.toString());
System.out.println(water3.toString());
System.out.println(water4.toString());
System.out.println(water5.toString());
}
}

运行图

Hongwei wechat
欢迎大家关注微信公众号:ghw1996