Java面向对象之super关键字怎么用
发表于:2024-10-01 作者:千家信息网编辑
千家信息网最后更新 2024年10月01日,这篇文章将为大家详细讲解有关Java面向对象之super关键字怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。/** this: 代表当前类的引用
千家信息网最后更新 2024年10月01日Java面向对象之super关键字怎么用
这篇文章将为大家详细讲解有关Java面向对象之super关键字怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
/** this: 代表当前类的引用 1. 当局部变量和成员变量同名时, 成员变量要加 this 限定 2. 实例化时 可以用 this 调用当前类的构造方法, 必须写在第一行 3. 可以用 this 调用当前类的 普通方法 super : 代表当前父类的引用 1. 实例化子类时, 可以用 super 调用父类的 非私有方法 2. 实例化子类时. 可以用 super 调用父类的 构造方法 , 必须写在第一行 3. 在子类的方法中 , 可以用 supe 调用父类的 非私有方法.*/public class Test1 { public static void main(String[] args) { // 实例化 农夫 Father father = new Father(); father.setName("农夫"); father.setAge(90); System.out.println(father.getAge() + " 岁 " + father.getName() + " 有 " + father.getWealth()); Father father = new Father("农夫", 90); System.out.println(father.getAge() + " 岁 " + father.getName() + " 有 " + father.getWealth()); Son son = new Son("农夫", 90); System.out.println("儿子知道父亲的 : " + son.getAge() + " 岁 " + son.getName() + " 有 " + son.getWealth()); son.work(); }}//父类class Father { private String name; private int age; private String wealth; public Father() { wealth = "100两黄金"; } public Father(String name, int age) { this(); this.name = name; //this.age=age; this.setAge(90); } public void work() { System.out.println("耕地"); } public String getWealth() { return wealth; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public String getName() { return name; }}//子类class Son extends Father { public Son(String name, int age) { super(name, age); //super.setName(name); //super.setAge(age); } public void work() { super.work(); System.out.println("儿子 寻找黄金宝藏"); System.out.println("只有通过自己的勤奋劳动, 才能得到果实 是最大的宝藏"); }}
关于"Java面向对象之super关键字怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
方法
农夫
子类
实例
变量
篇文章
关键
关键字
对象
一行
代表
儿子
宝藏
成员
更多
黄金
私有
不错
实用
普通
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
江西网络安全应急指挥
数据库创建表时设置字段外键约束
沈阳安卓软件开发外包
形容网络安全卫士
元宇宙网络安全公司
数据库修改信息c语言实现
手机做服务器可以给电脑用吗
数据库中可以避免数据冗余吗
无尽服务器号
dota2线下赛服务器
肥东网络技术服务联系人
召开网络安全联席会议
烟草多举措加强网络安全
数据库原理复合查询
adodb怎么连接数据库
电脑专业软件开发
江苏众道生鲜软件开发
wind中pe数据库
逃离塔科夫俄区选什么服务器
php 多服务器
安卓系统 有数据库吗
档案数据库建设实施方案
网络技术战队
网络安全保护自身权益
est是哪个数据库的
金星软件开发
沈阳软件开发员待遇
服装数据库系统分析报告
网络技术方面有哪些
朝阳软件开发哪家好