继承的好处
继承弊端
什么时候用继承
super关键字的用法和this关键字的用法相似
概述:在继承体系中,子类出现了和父类中一模一样的方法声明
应用场景
当子类需要父类的功能,而功能主体子类有自己特有内容,可以重写父类中的方法,这样,即沿袭了父类的功能,有定义了子类特有的内容
示例:
父类
public class OldPhone {
public void call(String name){
System.out.println("给"+name+"打电话");
}
public void speak(){
System.out.println("说英文");
}
}
子类
public class NewPhone extends OldPhone {
public void speak(){
super.speak();
System.out.println("说中文");
}
}
测试类
public class Test {
public static void main(String[] args) {
NewPhone newPhone = new NewPhone();
newPhone.speak();
newPhone.call("澜");
}
}
注意:
继承中所有的构造方法默认都会访问父类中无参的构造方法
为什么?
怎么初始化?
**注意:**如果我们编写的类,没有手动指定父类,系统也会自动继承Object(Java继承体系中的最顶层父类)
示例:
父类Person.java
package gouzao;
public class Student extends Person{
private int score;
public Student(){
//子类初始化之前,一定要先完成父类数据的初始化
//子类初始化之前,一定要先访问到父类的构造方法,完成父类数据的初始化
//系统在每一个构造方法中,默认隐藏一句代码super();
System.out.println("我是子类的无参构造");
}
public Student(int score){
this.score=score;
System.out.println("我是子类的带参构造");
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
子类Student.java
package gouzao;
public class Student extends Person{
private int score;
public Student(){
//子类初始化之前,一定要先完成父类数据的初始化
//子类初始化之前,一定要先访问到父类的构造方法,完成父类数据的初始化
//系统在每一个构造方法中,默认隐藏一句代码super();
System.out.println("我是子类的无参构造");
}
public Student(int score){
this.score=score;
System.out.println("我是子类的带参构造");
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
测试类Test.java
package gouzao;
public class Test {
public static void main(String[] args) {
Student stu = new Student();
Student stu1 = new Student(100);
}
}
如果父类中没有空参构造方法,只有带参构造方法,会出错,解决办法:
需求:定义猫类(Cat)和狗类(Dog)
猫类成员方法:eat(猫吃鱼)drink(喝水…)
狗类成员方法:eat(狗吃肉)drink(喝水…)
分析:
Animal类:
package chouxiang;
public abstract class Animal {
public void drink(){
System.out.println("喝水");
}
public abstract void eat();
}
Cat类:
package chouxiang;
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
Dog类:
package chouxiang;
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("狗吃肉");
}
}
测试类Test:
package chouxiang;
public class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.drink();
Cat c = new Cat();
c.eat();
c.drink();
}
}
final修饰的特点
final修饰成员变量,初始化时机
在java中,使用{}括起来的代码块被称为代码块
分类:
我们计划为一个电器销售公司制作一套系统,公司的主要业务是销售一些家用电器,例如:电冰箱、洗衣机等。
冰箱类:
洗衣机类:
请根据上述类的设计,抽取父类“电器类”,并代码实现父类“电器类”、子类“冰箱类”,“洗衣机类”;
父类Dianqi.java
package lian1;
public class Dianqi {
private String brand;
private String version;
private String color;
private double price;
public Dianqi() {
}
public Dianqi(String brand, String version, String color, double price) {
this.brand = brand;
this.version = version;
this.color = color;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
子类Refrigerator.java
package lian1;
public class Refrigerator extends Dianqi{
private String doorStyle;
private String wayRefrigeration;
public Refrigerator(){
}
public Refrigerator(String brand, String version, String color, double price, String doorStyle, String wayRefrigeration) {
super(brand, version, color, price);
this.doorStyle = doorStyle;
this.wayRefrigeration = wayRefrigeration;
}
public String getDoorStyle() {
return doorStyle;
}
public void setDoorStyle(String doorStyle) {
this.doorStyle = doorStyle;
}
public String getWayRefrigeration() {
return wayRefrigeration;
}
public void setWayRefrigeration(String wayRefrigeration) {
this.wayRefrigeration = wayRefrigeration;
}
}
子类Washer.java
package lian1;
public class Washer extends Dianqi {
private String powerStyle;
private int volume;
public Washer(String brand, String version, String color, double price, String powerStyle, int volume) {
super(brand, version, color, price);
this.powerStyle = powerStyle;
this.volume = volume;
}
public Washer(){
}
public String getPowerStyle() {
return powerStyle;
}
public void setPowerStyle(String powerStyle) {
this.powerStyle = powerStyle;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
}
测试类
package lian1;
public class DianqiTest {
public static void main(String[] args) {
Refrigerator rg = new Refrigerator("格力","暂无","灰色",1999.99,
"单门","不详");
Washer ws = new Washer("海尔","暂无","白色",999.99,"不详",50);
System.out.println(rg.getBrand()+" "+rg.getVersion()+" "+rg.getColor()+" "+rg.getPrice()
+" "+rg.getDoorStyle()+" "+rg.getWayRefrigeration());
System.out.println(ws.getBrand()+" "+ws.getVersion()+" "+ws.getColor()+" "+ws.getPrice()+" "
+ws.getPowerStyle()+" "+ws.getVolume());
}
}
我们计划为一所体育学校设计一套系统,需要记录以下人员的信息:
教练员:
运动员:
请根据需求,设计、并编码实现:父类“人员类”,子类“教练员类”、子类“运动员类”,并包含共有的属性和行为的定义,由于运动员和教练员的吃饭的内容不同,所以需要设计为“抽象方法”。
父类Person.java
package lian3;
public abstract class Person {
private String name;
private String gent;
private int age;
public abstract void eat();
public Person(String name, String gent, int age) {
this.name = name;
this.gent = gent;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGent() {
return gent;
}
public void setGent(String gent) {
this.gent = gent;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
子类Trainer.java
package lian3;
public class Trainer extends Person{
private String yid;
public Trainer(String name, String gent, int age, String yid) {
super(name, gent, age);
this.yid = yid;
}
public String getYid() {
return yid;
}
public void setYid(String yid) {
this.yid = yid;
}
@Override
public void eat() {
System.out.println("教练"+getName()+"正在吃工作餐,他的员工编号为"+getYid()+",年龄为"+getAge());
}
}
子类Sportsman.java
package lian3;
public class Sportsman extends Person {
private String xid;
public Sportsman(String name, String gent, int age, String xid) {
super(name, gent, age);
this.xid = xid;
}
public String getXid() {
return xid;
}
public void setXid(String xid) {
this.xid = xid;
}
@Override
public void eat() {
System.out.println("运动员"+getName()+"正在吃营养餐,他的学员编号为"+getXid()+",年龄为"+getAge());
}
}
测试类
package lian3;
import jicheng.Tager;
public class PersonTest {
public static void main(String[] args) {
Trainer t = new Trainer("老王","男",36,"20201467");
t.eat();
Sportsman s = new Sportsman("小王","男",22,"20200123");
s.eat();
}
}
某公司的系统中需要记录两类员工:
员工类:
经理类:
任何员工发布“通知”都统一使用以下格式:
通知:
xxxxxxxxx
xxxxxxxxx
必胜环球科技有限公司
父类tongzhi.java
package lian4;
public abstract class Tongzhi {
private String id;
private String name;
private int age;
public void tongzhi(){
System.out.println("通知:");
body();
System.out.println("\t\t必胜环球科技有限公司");
}
public abstract void body();
public Tongzhi(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
子类Jingli.java
package lian4;
public class Jingli extends Tongzhi{
private int jine;
public Jingli(String id, String name, int age, int jine) {
super(id, name, age);
this.jine = jine;
}
public int getJine() {
return jine;
}
public void setJine(int jine) {
this.jine = jine;
}
@Override
public void body() {
System.out.println("编号为:"+getId()+"的"+getAge()+"岁的经理"+getName()+"拿着"+jine+"元奖金发布了通知...");
}
}
子类Yuangong.java
package lian4;
public class Yuangong extends Tongzhi {
public Yuangong(String id, String name, int age) {
super(id, name, age);
}
@Override
public void body() {
System.out.println("编号为:"+getId()+"的"+getAge()+"岁的员工"+getName()+"发布了通知...");
}
}
测试类Test.java
package lian4;
public class Test {
public static void main(String[] args) {
Jingli j = new Jingli("20201216","老王",32,10000);
j.body();
Yuangong y = new Yuangong("20201612","小王",25);
y.body();
}
}