1錛氬瓙綾繪柟娉曚紭鍏堟绱㈠茍浣跨敤瀛愮被鐨勬柟娉?br />
2錛氬瓙綾諱笌鐖剁被鐨勫睘鎬ч兘鏄瓨鍦紝寮曠敤綾誨瀷鏄埗綾繪椂灝變嬌鐢ㄧ埗綾誨睘鎬э紝鏄瓙綾誨氨浣跨敤瀛愮被灞炴?br />
3
錛氳皟鐢ㄧ埗綾繪柟娉?super.method())錛岀埗綾繪柟娉曚腑錛?br />
璋冪敤鏂規(guī)硶錛屼紭鍏堟悳绱㈠瓙綾諱腑鏄惁鏈夎鏂規(guī)硶錛岀劧鍚庢槸鐖剁被鐨勶紱
璋冪敤灞炴э紝鍒欐槸浣跨敤鐖剁被鐨勫睘鎬с?br />
1
class Tree
{
2
int i = 3, j = 5;
3
4
public Tree()
{
5
this.show("Tree");
6
}
7
8
protected void show(String s)
{
9
System.out.println(s);
10
}
11
12
public void t()
{
13
this.show("Tree, t");
14
System.out.println(this.i);
15
System.out.println(this.j);
16
}
17
}
18
19
class Leaf extends Tree
{
20
int i = 10;
21
22
public Leaf ()
{
23
this.show("Leaf");
24
}
25
26
public void show(String s)
{
27
System.out.println(s + "\t\tLeaf");
28
}
29
30
public void t()
{
31
show("Leaf, t");
32
System.out.println(this.i);
33
System.out.println(this.j);
34
System.out.println();
35
super.t();
36
}
37
38
public static void main(String args[])
{
39
Leaf l = new Leaf();
40
l.t();
41
42
System.out.println("------------------");
43
System.out.println(l.i);
44
Tree t = new Leaf();
45
l = (Leaf) t;
46
System.out.println(t.i);
47
System.out.println(l.i);
48
}
49
}
50
鎵撳嵃緇撴灉錛?br />
Tree Leaf
Leaf Leaf
Leaf, t Leaf
10
5
Tree, t Leaf
3
5
------------------
10
Tree Leaf
Leaf Leaf
3
10

]]>