要求:
1.掌握继承的优点与实现
2.掌握子类重写父类的方法
3.掌握继承下构造方法的执行过程
4.掌握抽象类和抽象方法的使用
5.掌握final 修饰属性、方法和类
父类与子是 is_a 的关系
super 关键字访问父类成员:
1.super只能出现在子类方法和构造方法中
2.super调用构造方法时,只能是第一句
3.super 不能访问父类的private 成员
类不希望被继承 方法不希望被改写 属性值不希望被修改 ——final
优化电子宠物系统:
A:未使用super
Pet
package Animal2; public class Pet { private String name; private int health; private int love; public void print() { } 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; } }
Dog:
package Animal2; public class Dog extends Pet { String strain; public void print() { System.out.println("宠物的自白"); System.out.println("我的名字叫" + this.getName() + ",健康值是" + this.getHealth() + "和主人的亲密度是" + this.getLove() + ",我是一只酷酷的" + strain); } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } }
Penguin:
package Animal2; public class Penguin extends Pet { String sex; public void print() { System.out.println("宠物的自白"); System.out.println( "我的名字叫" + this.getName() + ",健康值是" + this.getHealth() + "和主人的亲密度是" + this.getLove() + ",性别是" + sex); } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
B:使用super
Pet
package Animal2; 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 void print() { } 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; } }
Dog;
package Animal2; public class Dog extends Pet { String strain; public Dog(String name, int health, int love, String strain) { super(name, health, love); this.strain = strain; } public Dog() { // TODO Auto-generated constructor stub } public void print() { System.out.println("宠物的自白"); System.out.println("我的名字叫" + this.getName() + ",健康值是" + this.getHealth() + "和主人的亲密度是" + this.getLove() + ",我是一只酷酷的" + strain); } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } }
Penguin;
package Animal2; public class Dog extends Pet { String strain; public Dog(String name, int health, int love, String strain) { super(name, health, love); this.strain = strain; } public Dog() { // TODO Auto-generated constructor stub } public void print() { System.out.println("宠物的自白"); System.out.println("我的名字叫" + this.getName() + ",健康值是" + this.getHealth() + "和主人的亲密度是" + this.getLove() + ",我是一只酷酷的" + strain); } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } }
Text:
package Animal2; import java.util.Scanner; public class Text { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("欢迎来到宠物店!"); System.out.println("请输入你要领养的宠物的名字:"); String name; int num; name = input.next(); System.out.println("Please choose the strain you like(1.狗狗 2.企鹅)"); num = input.nextInt(); if (num == 1) { System.out.println("请选择宠物的品种:(1.聪明的拉布多犬 2.酷酷的雪纳瑞)"); Dog dog = new Dog();// 使用构造 dog.setName(name); System.out.println(dog.getName()); dog.print(); } else { Penguin pen = new Penguin(); pen.setName(name); System.out.println("选择企鹅的性别:1.Q仔 2.Q妹"); num = input.nextInt(); if (num == 1) pen.setSex("Q仔"); else pen.setSex("Q妹"); pen.print(); } } }
由于单独定义Pet对象没有意义,所以抽象画Pet,抽象化方法,改变Pet
package Animal2; public abstract 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 abstract void print(); 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; } }
实现汽车租赁计价功能:
1.客车轿车都属于车 Vehicle
2.车分轿车与客车