当前位置: 首页 > news >正文

super关键字

super的基本语法

  1. super.xxxxx 作为基本格式
  2. super可以访问父类中非私有的属性和方法。
  3. super访问父类的构造器,只能在子类构造器的第一句出现,而且只出现一次。

super使用细节

在这里插入图片描述

在子类通过super访问父类的方法/成员

  1. 通过super访问一个子类和父类同名的方法/成员,会跳过当前类,直接去父类去找,如果不加super,就会现在这个类找,找不到再去父类。

  2. 通过子类去调用构造器,加了super也是直接去父类中找。

  3. this.成员/变量 = 不加this或super直接调用成员/变量

案例的code如下,我们在父类,子类中都创建有相同名字的方法成员

测试结果与上述结论一致:

father.java

public class father {father() {}father(String name, int age) {}father(String name) {}public void Cal() {System.out.println("这是father类中的Cal调用");}public String getName() {return this.m_Name;}protected int getTresure() {return this.m_Tresure;}private String[] getWife() {return this.m_Wifes;}public String m_Name = "老王";public String m_Flag = "father";protected int m_Tresure = 1_000_000;private String[] m_Wifes = {"丽丽", "大雷"};}

Son.java

public class Son extends father{public void hi() {System.out.println(super.m_Name);System.out.println(super.m_Tresure);System.out.println(super.getTresure());System.out.println(super.getName());}public Son() {super("sdf", 100);}public void Cal() {System.out.println("这是Son类中的Cal调用");}public void test01() {Cal();this.Cal();super.Cal();}public void test02() {System.out.println(m_Flag);System.out.println(this.m_Flag);System.out.println(super.m_Flag);}public String m_Flag = "Son";}

super01作为主函数调用

public class Super01 {public static void main(String[] args) {Son s1 = new Son();s1.test02();}
}

相关文章:

  • 测试--自动化测试函数
  • 会议动态|第十五届亚太燃烧学术年会精彩探析
  • 安装Anaconda后无jupyter解决方法
  • 新时代的可观测性:拥抱 GuanceDB 3.0
  • ISO 26262-5 硬件详细设计
  • (1) 查看端口状态
  • 【cursor指南】cursor免费续杯pro会员试用
  • Model 复现系列(一)OpenVLA
  • 【11408学习记录】考研英语辞职信写作三步法:真题精讲+妙句活用+范文模板
  • reboot命令和systemctl reboot命令区别(后者优雅重启)(Systemd系统中reboot实际上是systemctl reboot的软链接)
  • ai agent(智能体)开发 python高级应用7: crawl4ai 0.6.3 加re正则表达式 获取百度中含有 韩立的图片要求横屏图片
  • Java线程:多任务进行
  • 不同类型桥梁的无人机检测内容及技术难度
  • 无人机遥控器光纤通信模块技术要点!
  • 12.vue整合springboot首页显示数据库表-实现按钮:【添加修改删除查询】
  • SpringBoot-1-入门概念介绍和第一个Spring Boot项目
  • @RequestParam 和 @RequestBody、HttpServletrequest 与HttpServletResponse
  • 计算机网络-HTTP与HTTPS
  • unigui 监听控件的js事件
  • Agent的工作原理是什么?一文详解Agent的工作原理
  • 大巴车高速上撞山致2死2伤,广东肇庆警方通报
  • 电子凭证会计数据标准推广至全国
  • 消费维权周报丨上周涉汽车类投诉较多,涉加油“跳枪”等问题
  • 美俄亥俄州北部发生火车撞人事故,致2人死亡、至少1人失踪
  • 人民日报评论员:党政机关要带头过紧日子
  • 江苏疾控:下设部门无“病毒研究所”,常荣山非本单位工作人员