- 继承
- super 关键字
- protected
- 继承的原则:
- 继承的特点(JAVA):
- 向上转型和向下转型
- 重写override 覆盖
- abstract 抽象
- 练习
封装 继承 多态 面向对象的三大特征
继承 extends
语法:
class A extends B{
}
B 父类 // 不能访问子类中的方法,自己的方法随便用
// 负责公共的属性和方法
//父类的无参构造建议写上
A 子类 // 即可以用自己的方法,也可以用父类的方法
// 特有的属性和方法
访问控制符 : protected 子类可以使用父类中的
//父类
public class Employee {
protected String no;
protected String name;
protected double baseMoney;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBaseMoney() {
return baseMoney;
}
public void setBaseMoney(double baseMoney) {
this.baseMoney = baseMoney;
}
}
//子类
public class PM extends Employee{
private double managerMoney;
public PM() {
}
public PM(String no, String name, double baseMoney, double managerMoney) {
this.no = no;
this.name = name;
this.baseMoney = baseMoney;
this.managerMoney = managerMoney;
}
public double getManagerMoney() {
return managerMoney;
}
public void setManagerMoney(double managerMoney) {
this.managerMoney = managerMoney;
}
}
super 关键字继承之后,创建一个子类对象的程序;
父类属性初始化
父类构造
子类属性初始化
子类构造
(先父后子)
父类的
super()
super.属性
super.方法()
子类中的super()内可写父类的有参构造
public class Pet {
protected String nickname;
public void show() {
System.out.println("昵称:"+this.nickname);
System.out.println("亲密度:"+this.love);
}
}
public class Dog extends Pet {
private String nickname;
public void show() {
super.show();//调用父类的show()
System.out.println("品种:"+this.strain);
System.out.println(this.nickname); //调用子类的nickname
System.out.println(super.nickname);//调用父类的nickname
//当子类没有love时,this.love和super.love都是访问父类的属性
}
}
protected
受保护的,父类中的属性和方法
| 类内部 | 同包 子类 | 包外 子类 | 其他类 | |
|---|---|---|---|---|
| 1、public | 可以使用 | 可以使用 | 可以使用 | 可以使用 |
| 2、protected | 可以使用 | 可以使用 | (子类)可以使用 | 不可使用 |
| 3、default | 可以使用 | 可以使用 | 不可使用 | |
| 4、private | 可以使用 | 不可使用 | 不可使用 | 不可使用 |
子类从父类继承不了什么?
继承的原则:1、private 子类成员继承不了,
2、default 修饰的成员,保外的子类无法继承
3、构造方法,子类继承不了父类(子类必须得有构造方法)
除了以上三点,父类所有子类都能继承
继承的特点(JAVA): 子类 is a 父类(子类是一个父类)
表达的是类的本质
1、单继承,只有一个父类
2、传递性,任何一个类,路偶没有显式写出来继承谁,继承自Object
继承的作用:少些代码,公共的属性和方法
向上转型和向下转型子类比父类更强大,父类可以做的事情,子类都可以做
里式替换原则:所有父类出现的,都可以用子类代替
Pet p =new Dog(); //向上转型 自动进行的转换
//面向父类编程
//无法访问子类特有的属性和方法
Pet[] pets = new pet[3];
pets[0]= new Dog(参数);
//向下转型 强制转换
Dog d = (Dog)p;
//可以访问子类特有的属性和方法
重写override 覆盖向上转型:
父类类型 对象 = new 子类类型();
自行进行
好处:面向父类编程,便于程序的构架和程序的扩展
不好:不能使用子类特有的属性和方法
向下转型:
子类类型 对象 = (子类类型)对象:
强制进行
好处:使用子类特有的属性和方法
不好:容易引发ClassCastException
转换前先判断
A instanceof B类型-------如果A是B类型或者B的子类,都会true
Pet[] pets=new Pet[3]; pets[0]=new Dog("拉布拉多","毛毛",90); pets[1]=new Cat("咪咪",100); for (int i = 0; i < pets.length; i++) { Pet pet=pets[i]; //instanceof 判断某一个对象是否是某一种类型,当对象是这种类型或这种类型的子类,都返回true if(pet instanceof Dog) { Dog dog=(Dog)pet; //ClassCastException类型转换异常 dog.run(); }else if(pet instanceof Cat) { Cat c=(Cat)pet; c.sleep(); } }
abstract 抽象 在继承的基础上,子类拥有和父类一样的方法
重写的规则:
1、方法名一样
2、参数列表一样
3、(子类)访问控制权限可以放大
4、(子类)返回值类型可以缩小
重写不是必须的
为什么需要重写?
1、程序扩展的角度
2、方便去实现多态
public class Employee { protected double baseMoney; public double getSalary() { return 0; } } // public class SE extends Employee{ public double getSalary() { return this.baseMoney; } } // public class PM extends Employee{ private double managerMoney; public double getSalary() { return baseMoney+managerMoney; } }
练习修饰一个方法,就叫抽象方法
没有方法体,没有{}
抽象方法必须定义在抽象类中
修饰类,抽象类
一旦类是抽象的,要求子类必须重写,除非子类也是抽象的
抽象类不能实例化
使用场景:方法体无法描述时
public abstract void eat();能不能有构造方法?要,给子类使用
抽象类中是可以没有抽象方法?可以
抽象类中可以有非抽象方法么?可以
abatract可以修饰属性吗?不可以
使用场景: 1、禁止一个类实例化
2、描述一个抽象的概念
3、类中有抽象方法
public abstract class Pet { public Pet() { } public Pet(String nickname, int love) { this.nickname = nickname; this.love = love; } public abstract void eat(); }
public class Vehicle {
protected String brand;
protected String color;
public Vehicle() {
}
public Vehicle(String brand, String color) {
this.brand = brand;
this.color = color;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void Run() {
System.out.println("我已经开动了");
}
public void showInfo() {
System.out.println("商标:" + this.brand);
System.out.println("颜色:" + this.color);
}
}
public class Car extends Vehicle {
private int seats;
public Car(String brand, String color, int seats) {
super(brand, color);
this.seats = seats;
}
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
public void showCar() {
// super.Run();
super.showInfo();
System.out.println("座位:" + this.seats);
}
}
public class Truck extends Vehicle {
private float load;
public Truck(String brand, String color, float load) {
super(brand, color);
this.load = load;
}
public float getLoad() {
return load;
}
public void setLoad(float load) {
this.load = load;
}
public void showTruck() {
super.showInfo();
System.out.println("载重:" + this.load);
super.Run();
}
}
public class Test1 {
public static void main(String[] args) {
Car car = new Car("奥迪","蓝色",10);
car.showCar();
System.out.println("-----------------------");
Truck tru = new Truck("皮卡","白色",1800);
tru.showTruck();
}
}
public class Pet {
protected String nickname;
protected int health;
public Pet() {
}
public Pet(String nickname, int health) {
super();
this.nickname = nickname;
this.health = health;
}
public void eat() {
}
public void show() {
System.out.println("昵称:" + this.nickname);
System.out.println("健康值:" + this.health);
}
}
public class Dog extends Pet {
private String strain;
private String food;
public Dog(String nickname, int health, String strain, String food) {
super(nickname, health);
this.strain = strain;
this.food = food;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
@Override
public void eat() {
// super.eat();
System.out.println(this.nickname + "正在吃" + this.food);
}
public void display() {
System.out.println("玩飞盘,健康值:" + (super.health + 20));
System.out.println("生病,健康值:" + (super.health - 20));
}
}
public class Cat extends Pet {
private String food;
public Cat(String nickname, int health, String food) {
super(nickname, health);
this.food = food;
}
@Override
public void eat() {
super.eat();
System.out.println(this.nickname + "正在吃" + this.food);
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
}
public class TestPet {
public static void main(String[] args) {
Dog dog = new Dog("花花", 98, "哈士奇", "牛肉棒");
dog.show();
dog.display();
dog.eat();
System.out.println("-----------");
Cat cat = new Cat("瓶盖", 96, "猫罐头");
cat.show();
cat.eat();
}
}
public abstract class Vehicle1 {
public String countOfWheels() {
return countOfWheels();
}
}
public class Car extends Vehicle1 {
@Override
public String countOfWheels() {
return "四轮车";
}
}
public class Motorbike extends Vehicle1 {
@Override
public String countOfWheels() {
return "双轮车";
}
}
public class TestVehicle {
public static void main(String[] args) {
Car car = new Car();
System.out.println(car.countOfWheels());
Motorbike mot = new Motorbike();
System.out.println(mot.countOfWheels());
}
}
public class Bank {
protected String cardnumber;
protected String password;
protected String name;
protected double money;
public Bank() {
// TODO Auto-generated constructor stub
}
public Bank(String cardnumber, String password, String name, double money) {
super();
this.cardnumber = cardnumber;
this.password = password;
this.name = name;
this.money = money;
}
public String getCardnumber() {
return cardnumber;
}
public void setCardnumber(String cardnumber) {
this.cardnumber = cardnumber;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return money;
}
public void setBalance(double money) {
this.money = money;
}
public void show() {
System.out.println("卡号:" + this.cardnumber);
System.out.println("密码:" + this.password);
System.out.println("姓名:" + this.name);
System.out.println("余额:" + this.money);
}
}
public class TheBank extends Bank {
public TheBank() {
}
public TheBank(String cardnumber, String password, String name, double money) {
super(cardnumber, password, name, money);
}
public void withDrawal(double bal) {// 取款
if (bal > money) {
System.out.println("余额不足");
} else {
money = money - bal;
System.out.println(this.name + "取款:" + bal + "n余额为:" + money);
}
}
public void tranSfer(double tra, Bank num) {// 转账
if (tra > money) {
System.out.println("余额不足,转账失败");
} else {
money = money - tra;
num.money = num.money + tra;//给新银行卡存钱
System.out.println(this.name + "转账给新卡:" + tra + "n新卡余额:" + num.money);
}
}
}
public class OtherBank extends Bank {
public OtherBank(String cardnumber, String password, String name, double money) {
super(cardnumber, password, name, money);
}
public void withDrawal(double bal) {// 转账
double poor = 0;
double b2 = 2;//手续费
double bl = bal + b2;//取款数+手续费
if (bl > money) {//取款数+手续费 如果大于余额
System.out.println("余额不足,请重新输入");
} else {
money = money - bl;
System.out.println(this.name + "取款:" + bal + "n转账手续费为:" + b2 + "元n余额为:" + money);
}
}
}
public class TestBank {
public static void main(String[] args) {
TheBank the = new TheBank("622262081111111", "zxc1223", "胖子", 10000);
the.show();
the.withDrawal(350);
TheBank the1 = new TheBank();//转账银行卡开新的空间
the.tranSfer(6000, the1);
System.out.println("------------------");
OtherBank oth = new OtherBank("622262081111111", "zxc1223", "胖子", 10000);
oth.show();
oth.withDrawal(3000);
}
}



