-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetergent.java
More file actions
50 lines (49 loc) · 1.46 KB
/
Detergent.java
File metadata and controls
50 lines (49 loc) · 1.46 KB
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
//: reusing/Detergent.java
// Inheritace syntax & properties.
class Cleanser {
private String s = "Cleanser"; //为了继承,一般的规则是将所有的数据成员都指定为private
public void append(String a){ // 将所有方法方法指定为public
s += a;
}
public void dilute() {
append(" dilute()");
}
public void apply() {
append(" apply()");
}
public void scrub() {
append(" scrub()");
}
public String toString(){
return s;
}
public static void main(String[] args) {
Cleanser x = new Cleanser();
x.dilute();
x.apply();
x.scrub();
System.out.println(x);
}
}
public class Detergent extends Cleanser {
// change a method:
public void scrub() { // 使用基类中定义的方法及对它进行修改是可行的
append(" Detergent.scrub()");
super.scrub(); // Java用super关键字 表示超类的意思 当前类就是从超类继承来的
} // 为此,表达式 super.scrub() 将调用基类版本的scrub() 方法
// Add methods to the interface:
public void foam() {
append(" foam()");
}
// Test the new class:
public static void main(String[] args) {
Detergent x = new Detergent();
x.dilute();
x.apply();
x.scrub();
x.foam();
System.out.println(x);
System.out.println("Testing base class:");
Cleanser.main(args);
}
}