Java在子类中调用super()方法
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值
好看请赞,养成习惯:) 本文来自博客园,作者:靠谱杨, 转载请注明原文链接:https://www.cnblogs.com/rainbow-1/p/13847141.html
欢迎来我的51CTO博客主页踩一踩 我的51CTO博客
文章中的公众号名称可能有误,请统一搜索:靠谱杨的秘密基地