当前位置:网站首页>Day018 Inheritance
Day018 Inheritance
2022-08-04 18:34:00 【Strange year】
目录
1、继承的使用
(1)使用继承
① 编写父类
[访问修饰符] class Pet {
//公共的属性和方法
}
②编写子类,继承父类
[访问修饰符] class Dog extends Pet {
//子类特有的属性和方法
}
注意:继承是Java中实现代码重用的重要手段之一.java中只支持单根继承,即一个类只能有一个直接父类.
2、继承的理解
(1)子类访问父类
① 访问父类构造方法
super();
super(name);
② 访问父类属性
super.name;
③ 访问父类方法
super.print()
注意:
(1)使用super关键字,super代表父类对象
(2)在子类构造方法中调用且必须是第一句
(3)不可以访问父类中定义为private的属性和方法
小结:
super关键字来访问父类的成员
super只能出现在子类的方法和构造方法中;
super调用构造方法时,只能是第一句;
super不能访问父类的private成员.
2、继承条件下的构造方法
2.1、继承条件下构造方法的调用规则 
2.2 子类继承父类的什么

2.3 Does the subclass inherit all the resources of the parent class??

2.4 小结

3、实例
创建一个Pet类作为父类
public class Pet {
private String name;
private int health;
private int love;
//添加构造方法
public Pet() {
}
public Pet(String name, int health, int love) {
this.name = name;
this.health = health;
this.love = love;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public void print(){
System.out.println("宠物信息");
System.out.println("昵称:"+this.getName()+"\n健康值:"+this.getHealth()+"\n亲密度:"+this.getLove()+"颜色:");
}
}
创建一个Dog类作为子类
public class Dog extends Pet {
//strain属性是DogAttributes unique to a class,None of the other classes
private String strain;
public Dog() {
}
public Dog(String name, int health, int love, String strain) {
super(name, health, love);
this.strain = strain;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
}创建一个Cat类作为子类
public class Cat extends Pet {
// 定义Cat类独有的属性
private String color;
public Cat() {
super();// 调用父类Pet类里的无参构造方法
}
public Cat(String name, int health, int love, String color) {
super(name, health, love);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}创建一个Penguin作为子类
public class Penguin {
private String name;
private int health;
private int love;
private String sex;
public Penguin() {
}
public Penguin(String name, int health, int love, String sex) {
this.name = name;
this.health = health;
this.love = love;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void print() {
System.out.println("宠物信息:昵称:" + this.getName() + ",健康值:"
+ this.getHealth() + ",亲密度:" + this.getLove());
}
}Finally create a test class for testing
public class Test {
public static void main(String[] args) {
// 创建Dog类对象
Dog dog1 = new Dog("旺财", 99, 99, "金毛");
//在Dog类中没有setName()方法,但是Dog类对象dog1是可以调用,因为Dog类继承了Pet类,所以dog1可以调用父类Pet类里的setName()方法
dog1.setName("来福");
dog1.print();
//创建Penguin类对象
Penguin penguin1 = new Penguin("QQ", 98, 100, "公");
penguin1.setHealth(100);
penguin1.print();
//创建Cat类对象
Cat cat1 = new Cat("Tom", 100, 80, "蓝色");
cat1.print();
//hashCode()在Cat里没有、在Cat的父类Pet类里也没有,hashCode()方法存在于Object类中,Object类是Pet类的默认父类,所以Cat类对象可以使用Pet类里的方法,也可以使用Pet类的父类里的方法
//声明一个类,如果没有指定其父类是谁,系统会让此类默认去继承Object类
int num =cat1.hashCode();
System.out.println(num);//1694203642
}
}边栏推荐
- linux下Mysql的简单操作
- 离线同步odps到mysql 中文乱码是因为?mysql已是utf8mb4
- Global electronics demand slows: Samsung's Vietnam plant significantly reduces capacity
- 2018年南海区小学生程序设计竞赛详细答案
- 数仓建模面试
- Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...
- How does the intelligent video surveillance platform EasyCVR use the interface to export iframe addresses in batches?
- Route lazy loading
- 阿里云国际版使用ROS搭建WordPress教程
- 单行、多行文本超出显示省略号
猜你喜欢

报道称任天堂在2023年3月前不会推出任何新硬件产品

关于使用腾讯云HiFlow场景连接器每天提醒签到打卡

解决错误:The package-lock.json file was created with an old version of npm

LVS+NAT 负载均衡群集,NAT模式部署

Flink / Scala - 使用 RedisSink 存储数据

工业元宇宙对工业带来的改变

火灾报警联网FC18中CAN光端机常见问题解答和使用指导

unity中实现ue眼球的渲染

Develop those things: How to obtain the traffic statistics of the monitoring site through the EasyCVR platform?

用Excel绘制统计图
随机推荐
PHP代码审计9—代码执行漏洞
方法的重写
Documentary on Security Reinforcement of Network Range Monitoring System (1)—SSL/TLS Encrypted Transmission of Log Data
【STM32】入门(五):串口TTL、RS232、RS485
Day018 继承
浅谈web网站架构演变过程
unity中实现ue眼球的渲染
2018年南海区小学生程序设计竞赛详细答案
如何让 JS 代码不可断点
作业8.3 线程同步互斥机制条件变量
Go language Go language, understand Go language file operation in one article
【填空题】130道面试填空题
关于使用腾讯云HiFlow场景连接器每天提醒签到打卡
Understanding of margin collapse and coincidence
合宙Cat1 4G模块Air724UG配置RNDIS网卡或PPP拨号,通过RNDIS网卡使开发板上网(以RV1126/1109开发板为例)
单行、多行文本超出显示省略号
企业即时通讯软件有哪些功能?对企业有什么帮助?
Nintendo won't launch any new hardware until March 2023, report says
How does the intelligent video surveillance platform EasyCVR use the interface to export iframe addresses in batches?
localstorage本地存储的方法