1 package varycode;
2 class Grandparent
3 {
4 public Grandparent()
5 {
6
7 System.out.println("GrandParent Created.");
8
9 }
10
11
12 public Grandparent(String string)
13 {
14
15 System.out.println("GrandParent Created.String:" + string);
16
17 }
18
19 }
20
21
22
23 class Parent extends Grandparent
24 {
25
26
27 public Parent()
28 {
29
30 //super("Hello.Grandparent.");
31
32 System.out.println("Parent Created");
33
34 // super("Hello.Grandparent.");
35
36 }
37
38 }
39
40
41
42 class Child extends Parent
43 {
44
45
46 public Child()
47 {
48
49 System.out.println("Child Created");
50
51 }
52
53 }
54
55
56
57 public class TestInherits
58 {
59
60
61 public static void main(String args[])
62 {
63
64 Child c = new Child();
65
66 }
67
68
結論:在子類中調用super()方法,必須把調用語句放在子類構造的第一句,構造函數是實作初始化功能的,從子類一定要開始向父類實作構造方法。
二、
1 package varycode;
2 public class ExplorationJDKSource {
3
4 /**
5 * @param args
6 */
7 public static void main(String[] args) {
8 System.out.println(new A());
9 }
10
11 }
12
13 class A{}
預設調用super類的hash值
好看請贊,養成習慣 :) ,作者:靠譜楊
關于筆者:我可能不是天才,但我會努力成為人才。
更多日常分享盡在我的VX公衆号:小楊的挨踢IT生活