配套視頻教程
問(wèn)題
寵物餓了芥备,需要主人給寵物喂食
不同寵物吃的東西不一樣统锤,狗吃骨頭流济,企鵝吃魚(yú)
不同寵物恢復(fù)后體力值不一樣
狗狗類(lèi)
增加狗狗吃食的方法企鵝類(lèi)
增加企鵝吃食的方法創(chuàng)建主人類(lèi)
編寫(xiě)給狗狗喂食的方法
編寫(xiě)給企鵝喂食的方法編寫(xiě)測(cè)試方法
調(diào)用主人類(lèi)給狗狗喂的方法
調(diào)用主人類(lèi)給企鵝喂的方法
寵物父類(lèi)
public abstract class Pet {
private String name = "無(wú)名氏";// 昵稱(chēng)
private int health = 100;// 健康值
private int love = 0;// 親密度
/**
* 抽象方法eat(),負(fù)責(zé)寵物吃飯功能。
*/
public abstract void eat();
/**
* 有參構(gòu)造方法蛮寂。
* @param name 昵稱(chēng)
*/
public Pet(){
}
public Pet(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public void setHealth(int health) {
this.health = health;
}
public void setLove(int love) {
this.love = love;
}
public String getName() {
return name;
}
public int getHealth() {
return health;
}
public int getLove() {
return love;
}
/**
* 輸出寵物信息。
*/
public void print() {
System.out.println("寵物的自白:\n我的名字叫" + this.name +
"易茬,健康值是" + this.health + "酬蹋,和主人的親密度是"
+ this.love + "。");
}
}
狗類(lèi)
public class Dog extends Pet {
private String strain;// 品種
/**
* 有參構(gòu)造方法抽莱。
* @param name 昵稱(chēng)
* @param strain 品種
*/
public Dog(String name, String strain) {
super(name);
this.strain = strain;
}
public String getStrain() {
return strain;
}
/**
* 重寫(xiě)父類(lèi)的print方法范抓。
*/
public void print(){
super.print(); //調(diào)用父類(lèi)的print方法
System.out.println("我是一只 " + this.strain + "。");
}
/**
* 實(shí)現(xiàn)吃食方法食铐。
*/
public void eat() {
if(getHealth()>=100){
System.out.println("狗狗"+this.getName() +"吃飽了尉咕,不需要喂食了!");
}else{
this.setHealth(this.getHealth()+3);
System.out.println("狗狗"+this.getName() + "吃飽啦璃岳!健康值增加3年缎。");
}
}
}
企鵝類(lèi)
public class Penguin extends Pet {
private String sex;// 性別
/**
* 有參構(gòu)造方法。
* @param name 昵稱(chēng)
* @param sex 性別
*/
public Penguin(String name, String sex) {
super(name);
this.sex = sex;
}
public String getSex() {
return sex;
}
/**
* 重寫(xiě)父類(lèi)的print方法铃慷。
*/
public void print() {
super.print();
System.out.println("性別是 " + this.sex + "单芜。");
}
/**
* 實(shí)現(xiàn)吃食方法。
*/
public void eat() {
if(getHealth()>=100){
System.out.println("企鵝"+this.getName() +"吃飽了犁柜,不需要喂食了洲鸠!");
}else{
this.setHealth(this.getHealth()+5);
System.out.println("企鵝"+this.getName() + "吃飽啦!健康值增加3。");
}
}
}
主人類(lèi)
public class Master {
private String name = "";// 主人名字
private int money = 0; // 元寶數(shù)
/**
* 有參構(gòu)造方法扒腕。
* @param name 主人名字
* @param money 元寶數(shù)
*/
public Master(String name, int money) {
this.name = name;
this.money = money;
}
public void setName(String name) {
this.name = name;
}
public void setMoney(int money) {
this.money = money;
}
public int getMoney() {
return money;
}
public String getName() {
return name;
}
/**
* 主人給Dog喂食绢淀。
*/
public void feed(Dog dog) {
dog.eat();
}
/**
* 主人給Penguin喂食。
*/
public void feed(Penguin pgn) {
pgn.eat();
}
}
測(cè)試類(lèi)
public class Test {
public static void main(String[] args) {
Dog dog = new Dog("歐歐", "雪娜瑞");
dog.setHealth(80); //設(shè)置健康值瘾腰,以便正常喂食
Penguin pgn = new Penguin("楠楠", "Q妹");
Master master=new Master("王先生",100);
master.feed(dog);//主人給狗狗喂食
master.feed(pgn);//主人給企鵝喂食
pgn.setHealth(80); //設(shè)置健康值皆的,以便正常喂食
master.feed(pgn);//主人再次給企鵝喂食
}
}
如果再領(lǐng)養(yǎng)新種類(lèi)XXX的寵物,就需要給XXX喂食蹋盆,怎么辦费薄?
- 添加X(jué)XX類(lèi),繼承Pet類(lèi)栖雾,實(shí)現(xiàn)吃食方法
- 修改Master類(lèi)楞抡,添加給XXX喂食的方法
public class Master {
public void feed( Dog dog ) {
dog.eat();
}
public void feed( Penguin pgn ) {
pgn.eat();
}
public void feed( XXX xxx ) {
xxx.eat();
}
… …
}
頻繁修改代碼,代碼可擴(kuò)展性析藕、可維護(hù)性差召廷,如何優(yōu)化?
使用多態(tài)優(yōu)化設(shè)計(jì)
參數(shù)都是Pet類(lèi)的子類(lèi)
可否使用一個(gè)feed(Pet pet)實(shí)現(xiàn)對(duì)所有寵物的喂食账胧?
多態(tài):同一個(gè)引用類(lèi)型竞慢,使用不同的實(shí)例而執(zhí)行不同操作
使用多態(tài)實(shí)現(xiàn)思路
- 編寫(xiě)父類(lèi)
- 編寫(xiě)子類(lèi),子類(lèi)重寫(xiě)父類(lèi)方法
- 運(yùn)行時(shí)找爱,使用父類(lèi)的類(lèi)型梗顺,子類(lèi)的對(duì)象
Pet pet = new Dog();
使用父類(lèi)作為方法形參實(shí)現(xiàn)多態(tài)
使用多態(tài)優(yōu)化主人給寵物喂食
public class Master {
//使用父類(lèi)作為方法形參
public void feed( Pet pet ) {
pet.eat();
}
}
測(cè)試類(lèi)的改變
Pet pet = new Dog();
Master master = new Master();
master.feed( pet );//同一種操作方式,不同的操作對(duì)象
練習(xí)
使用多態(tài)實(shí)現(xiàn)喂養(yǎng)寵物功能
增加寵物貓并喂食车摄,其健康值增加4
計(jì)算一次租賃多輛汽車(chē)的總租金
在繼承章節(jié)汽車(chē)租賃系統(tǒng)的基礎(chǔ)上寺谤,實(shí)現(xiàn)計(jì)算多種車(chē)輛總租金的功能
現(xiàn)在有客戶(hù)租用
2輛寶馬
1輛別克商務(wù)艙
1輛金龍(34)座
租5天共多少租金?
解題思路
1吮播、創(chuàng)建車(chē)的對(duì)象变屁,放在數(shù)組中
MotoVehile[] motos = new MotoVehile[4];
motos[0] = new Car("寶馬550i","京NY28588");
motos[1] = new Car("寶馬550i","京NNN328");
motos[2] = new Car("別克林蔭大道","京NY28588");
motos[3] = new Bus("金龍",34);
2 循環(huán)調(diào)用calcRent()方法,計(jì)算總租金
public int calcTotalRent(MotoVehile[] motos,int days){
int totalRent = 0;
for(int i=0;i<motos.length;++i){
totalRent += motos[i].calRent(days);
}
return totalRent;
}
假設(shè)新增了一種對(duì)外出租的車(chē)輛類(lèi)型
新購(gòu)置了卡車(chē)意狠,根據(jù)噸位粟关,租金每噸每天50
對(duì)系統(tǒng)進(jìn)行擴(kuò)展,計(jì)算汽車(chē)租賃的總租金
解題思路
1.創(chuàng)建卡車(chē)類(lèi)环戈,實(shí)現(xiàn)calcRent ()方法
2.修改統(tǒng)計(jì)租金代碼
public abstract class MotoVehicle {
private String no;//車(chē)牌號(hào)
private String brand;//品牌
abstract int calRent(int days);//計(jì)算租金
public MotoVehicle()
{
}
public MotoVehicle(String no, String brand)
{
this.no = no;
this.brand = brand;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
public class Car extends MotoVehicle{
public Car(String no, String brand)
{
super(no,brand);
}
@Override
int calRent(int days) {
if(getBrand().equals("寶馬"))
{
return 500 * days;
}
else
{
return 600 * days;
}
}
}
public class Kache extends MotoVehicle {
private int dunwei;//噸位
public Kache(int dunwei)
{
this.dunwei = dunwei;
}
@Override
int calRent(int days) {
return 50*dunwei*days;
}
}
不用多態(tài)的實(shí)現(xiàn)方式
Car[] cars = new Car[3];
cars[0] = new Car("遼A12345","寶馬");
cars[1] = new Car("遼B22222","別克");
cars[2] = new Car("遼A62222","奔馳");
Kache[] kaches = new Kache[2];
kaches[0] = new Kache(1);//150
kaches[1] = new Kache(4);//600
int total = 0;
for(int i = 0; i < cars.length; i++)
{
total += cars[i].calRent(3);
}
for(int i = 0; i < kaches.length; i++)
{
total += kaches[i].calRent(3);
}
System.out.println("總租金" + total);//5850
使用多態(tài)實(shí)現(xiàn)
MotoVehicle[] motoVehicles = new MotoVehicle[5];
motoVehicles[0] = new Car("遼A12345","寶馬");
motoVehicles[1] = new Car("遼B22222","別克");
motoVehicles[2] = new Car("遼A62222","奔馳");
motoVehicles[3] = new Kache(1);
motoVehicles[4] = new Kache(4);
int total = 0;
for(int i = 0; i < motoVehicles.length; i++)
{
total += motoVehicles[i].calRent(3);
}
System.out.println("總租金" + total);
一個(gè)練習(xí)
工資支付系統(tǒng)
定義一個(gè)Employee抽象基類(lèi)(name)
公司有以下幾種員工:
開(kāi)發(fā)人員:工資計(jì)算方式闷板,每月固定工資
銷(xiāo)售人員:底薪+提成
硬件工程師:生產(chǎn)零件,每個(gè)50元
小時(shí)工:按工作時(shí)間付工資院塞,每小時(shí)30元
主類(lèi)(測(cè)試類(lèi))
創(chuàng)建不同類(lèi)型的6名員工對(duì)象遮晚,計(jì)算他們應(yīng)付的月工資之和
public abstract class Employee {
private String name;
public Employee(String name)
{
this.name = "@" + name + "@" ;
}
protected abstract int paySalary();
}
//開(kāi)發(fā)人員
public class Developer extends Employee{
private int salary;
public Developer(String name, int salary)
{
super(name);
this.salary = salary;
}
@Override//方法重寫(xiě)注解
protected int paySalary() {
return salary;
}
}
//銷(xiāo)售人員
public class SaleMan extends Employee{
private int salary;
private int comm;//提成
public SaleMan(String name, int salary, int comm)
{
super(name);
this.salary = salary;
this.comm = comm;
}
@Override
protected int paySalary() {
return salary + comm;
}
}
//硬件工程師
public class HardwareEngineer extends Employee {
private int productCount;//每月生產(chǎn)的零件數(shù)量
public HardwareEngineer(String name, int productCount)
{
super(name);
this.productCount = productCount;
}
@Override
protected int paySalary() {
return 50*productCount;
}
}
//小時(shí)工
public class HourlyWorker extends Employee{
private int hours;//這個(gè)月工作了多少小時(shí)
public HourlyWorker(String name, int hours)
{
super(name);
this.hours = hours;
}
@Override
protected int paySalary() {
return 30*hours;
}
}
public class Boss extends Employee {
public Boss(String name)
{
super(name);
}
@Override
protected int paySalary() {
return 200000;
}
}
public class Main {
public static void main(String[] args) {
// write your code here
Employee[] employees = new Employee[6];
employees[0] = new Developer("張二",5000);
employees[1] = new Developer("張三",5000);
employees[2] = new SaleMan("李四", 2000, 500);
employees[3] = new HardwareEngineer("王五", 200);
employees[4] = new HourlyWorker("馮六",200);
employees[5] = new Boss("趙本山");
//計(jì)算總工資
int total = 0;
for(int i = 0; i < employees.length; i++)
{
total += employees[i].paySalary();
}
System.out.println("總共需要支付" + total);
}
}