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值

复制代码

 

posted @   靠谱杨  阅读(149)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示