一、理解继承的作用并学会使用继承
这边的Dog与下面的Penguin有什么问题呢?如何优化呢
- name:String
- health:int
- love:int
- strain:String
----------------
+ print():void
+ getName():String
+ getHealth():int
+ getLove():int
+ getStrain():String
+ Dog()
}
package o915;
public class Pernguin {
- name:String
- health:int
- love:int
- sex:String
----------------
+ print():void
+ getName():String
+ getHealth():int
+ getLove():int
+ getSex():String
+Pernguin()
}
由此得出:
/* -------------------------------------------------------------------------------
* [US-EN]:
* (Attribution):
* By SHaoYuDeng.( Tao Mo Si ).Full ownership and all rights reserved.
* (File. Role):
* System name: ka
* 1.The source file [Pernguin.java] belongs to the underlying file.
* 2.You can only limit the scope of this project [JavaSrc].
* (The license):
* 1.This source document is not commercial, but it is not non-commercial,
* such as commercial need through the original author authorization,
* If no authorization is available.
* stop using it immediately and view related files.
* 2.User ONLY HAS THE RIGHT TO SET WITHOUT THE RIGHT TO modify!!
* (Contact):
* [Email]: 2412874626@qq.com
* [Phone]: + 86 17681306381
* Copyright (c) 2022. 9
* By SHaoYuDeng.( Tao Mo Si ).Full ownership and all rights reserved.
* @version: 1.0
* -------------------------------------------------------------------------------
*
*/
package o915;
public class Penguin {
private String name;
private int height;
private int wid;
private String sex;
public Penguin() {
}
public String getName() {
return name;
}
public Penguin setName(String name) {
this.name = name;
return this;
}
public int getHeight() {
return height;
}
public Penguin setHeight(int height) {
this.height = height;
return this;
}
public int getWid() {
return wid;
}
public Penguin setWid(int wid) {
this.wid = wid;
return this;
}
public String getSex() {
return sex;
}
public Penguin setSex(String sex) {
this.sex = sex;
return this;
}
public void print(){
System.out.println("信息输出:"+this.getName()+" "+this.getHeight()+" "+this.getWid());
}
public Penguin(String name, int height, int wid, String sex) {
super();
this.name = name;
this.height = height;
this.wid = wid;
this.sex = sex;
}
}
package o915;
public class Dog {
private String name;
private int height;
public int wid;
// strain
private String strain;
public Dog(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWid() {
return wid;
}
public void setWid(int wid) {
this.wid = wid;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public void print(){
System.out.println("信息输出:"+this.getName()+" "+this.getHeight()+" "+this.getWid());
}
public Dog(String name, int height, String strain){
this.name = name;
this.height= height;
this.strain = strain;
}
}
我们可以将重复的代码抽取道【父类】中并使用【继承】来优化设计
可以将dog和penguin里的类提取道Ani里
实列:
Ain文件
package o915.ol;
public class Ani {
// 将demo01包中Dog类和Penguin类中相同的代码提取到这个Animal类中
private String name;
private int health;
private int love;
public Ani() {
}
public Ani(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("宠物信息-->昵称:" + this.getName() + ",健康值:"
+ this.getHealth() + ",亲密度:" + this.getLove());
}
}
Penguin文件
package o915.ol;
public class Penguin extends Ani {
//定义企鹅类中特有的属性
private String sex;
public Penguin() {
super();//表示使用Animal类中的无参构造方法
}
public Penguin(String name, int health, int love, String sex) {
super(name, health, love);
this.sex = sex;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//定义Penguin类中特有的方法
public void swimming(){
System.out.println("企鹅会仰泳.......");
}
}
Dog文件
package o915.ol;
public class Dog extends Ani {
//在这个Dog类中只定义Dog类中特有的属性和方法,原来和Penguin类中相同的代码在Animal类中,通过继承获取,使用extends关键字来获取
private String strain;
public Dog() {
super();//表示使用Animal类中的无参构造方法
}
public Dog(String name, int health, int love, String strain) {
super(name, health, love);//表示使用Animal类中的有参构造方法
this.strain = strain;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
//定义Dog类中特有的方法
public void eat(){
System.out.println("狗吃骨头......");
}
public void printInfo(){
super.print();
System.out.println("品种:"+this.getStrain());
}
}
Test文件
package o915.ol;
public class Test {
public static void main(String[] args) {
//创建Dog类对象
Dog dog1 = new Dog();
dog1.setName("二哈");
dog1.setHealth(100);
dog1.setLove(100);
dog1.setStrain("喵咪");
//print()方法是Animal类中的
dog1.print();
//eat()方法是Dog类中的
dog1.eat();
dog1.printInfo();
Penguin p1 = new Penguin("企鹅", 99, 88, "公");
//print()方法是Animal类中的
p1.print();
//swimming()是Penguin类中的
p1.swimming();
}
}
信息台输出:
宠物信息-->昵称:二哈,健康值:100,亲密度:100
狗吃骨头......
宠物信息-->昵称:二哈,健康值:100,亲密度:100
品种:喵咪
宠物信息-->昵称:企鹅,健康值:99,亲密度:88
企鹅会仰泳.......
可以看到继承关系的复杂与好处,设顶的子类可以继承设置好的父类,从而获得信息的汇总与分发