/
ThisSuperTest.java
56 lines (41 loc) · 1.17 KB
/
ThisSuperTest.java
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
package play;
public class ThisSuperTest {
public static void main(String args[]){
Cow cow = new Cow();
System.out.println();
cow.speak();
}
}
/**
this-and-super.play 面向对象的this和super特性。
*/
class Mammal{
int weight;
public Mammal(){
System.out.println("Mammal() called");
this.weight = 100;
}
Mammal(int weight){
this(); //调用另一个构造函数
System.out.println("Mammal(int weight) called");
this.weight = weight;
this.speak(); //这里多态仍然会生效,实际调用的是Cow的speak方法
}
void speak(){
System.out.println("Mammal speaking...");
}
}
class Cow extends Mammal{
int weight = 300;
public Cow(){
super(200); //调用父类的构造函数
}
void speak(){
System.out.println("in Cow.speak");
System.out.println("super.weight : " + super.weight);
System.out.println("this.weight : " + this.weight);
System.out.println("call super.speak() : " );
super.speak();
System.out.println("Cow speak is : moo~~ moo~~");
}
}