具体实现代码
1.Print
package com.hpe.java;
public class Print {
public void print(){
System.out.println("打印!");
}
}
2.PrintBlack
package com.hpe.java;
/**
* 1. 用多态实现打印机
分为黑白打印机和彩色打印机
不同类型的打印机打印效果不同
* @author yang
*
*/
public class PrintBlack extends Print{
public void print(){
System.out.println("我是黑白打印机");
}
}
3.PrintColor
package com.hpe.java;
public class PrintColor {
public void print(){
System.out.println("我是彩色打印机");
}
}
4.TestPrint
package com.hpe.java;
public class TestPrint {
public static void main(String[] args) {
PrintBlack b=new PrintBlack();
b.print();
PrintColor c=new PrintColor();
c.print();
}
}
2、在课上案例的基础上实现与宠物玩耍功能
需求说明:
主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5
主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5
提示:
Dog类添加catchingFlyDisc()方法,实现接飞盘功能
Penguin类添加swimming()方法,实现游泳功能
主人添加play(Pet pet)方法
如果pet代表Dog就玩接飞盘游戏
如果pet代表Penguin就玩游泳游戏
具体实现代码
1.Dog
package com.hpe.java1;
/**
* 2、在课上案例的基础上实现与宠物玩耍功能
需求说明:
主人和狗狗玩接飞盘游戏,狗狗健康值减少10,与主人亲密度增加5
主人和企鹅玩游泳游戏,企鹅健康值减少10,与主人亲密度增加5
提示:
Dog类添加catchingFlyDisc()方法,实现接飞盘功能
Penguin类添加swimming()方法,实现游泳功能
主人添加play(Pet pet)方法
如果pet代表Dog就玩接飞盘游戏
如果pet代表Penguin就玩游泳游戏
* @author yang
*
*/
public class Dog extends Pet{
public void catchingFlyDisc(){
super.health=super.health+10;
super.intimacy=super.intimacy+5;
System.out.println("接飞盘");
}
public Dog(int health,int intimacy){
super(health,intimacy);
}
}
2.Host
package com.hpe.java1;
public class Host {
public void play(Pet p){
if(p instanceof Dog){
((Dog) p).catchingFlyDisc();
}else if(p instanceof Penguin){
((Penguin) p).swimming();
}
}
}
3.Penguin
package com.hpe.java1;
public class Penguin extends Pet{
public void swimming(){
super.health=super.health+10;
super.intimacy=super.intimacy+5;
System.out.println("游泳!");
}
public Penguin(int health,int intimacy){
super(health,intimacy);
}
}
4.Pet
package com.hpe.java1;
public class Pet {
protected int health;
protected int intimacy;
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getIntimacy() {
return intimacy;
}
public void setIntimacy(int intimacy) {
this.intimacy = intimacy;
}
public Pet(int health, int intimacy) {
super();
this.health = health;
this.intimacy = intimacy;
}
public Pet() {
super();
}
}
5.Test
package com.hpe.java1;
public class Test {
public static void main(String[] args) {
Pet p=new Dog(3, 5);
Pet p2=new Penguin(2, 6);
Host h=new Host();
h.play(p);
System.out.println("健康值:"+p.getHealth()+",亲密值:"+p.getIntimacy());
h.play(p2);
System.out.println("健康值:"+p2.getHealth()+",亲密值:"+p2.getIntimacy());
}
}
3、某汽车租赁公司出租多种车辆,车型及租金情况如下:
编写程序实现计算租赁价
分析
4、需求说明:
在前面汽车租赁系统的基础上,实现计算多种车辆总租金的功能。
现在有客户租用:
2辆宝马
1辆别克商务舱
1辆金龙(34)座
租5天共多少租金
5、需求说明:
新购置了卡车,根据吨位,租金每吨每天50
对系统进行扩展,计算汽车租赁的总租金
具体实现代码
1.Bus
package work_multi.d3;
/**
* 在前面汽车租赁系统的基础上,实现计算多种车辆总租金的功能。
现在有客户租用:
2辆宝马
1辆别克商务舱
1辆金龙(34)座
租5天共多少租金
5、需求说明:
新购置了卡车,根据吨位,租金每吨每天50
对系统进行扩展,计算汽车租赁的总租金
* @author yang
*
*/
public class Bus extends MotoVehicle{
private int SeatCount;
public int getSeatCount() {
return SeatCount;
}
public void setSeatCount(int seatCount) {
SeatCount = seatCount;
}
public Bus(int no,int seatCount) {
super(no);
SeatCount = seatCount;
}
public Bus() {
super();
}
}
2.Car
package work_multi.d3;
public class Car extends MotoVehicle{
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Car(int no,String type) {
super(no);
this.type = type;
}
public Car() {
super();
}
}
3.MotoVehicle
package work_multi.d3;
public class MotoVehicle {
private int no;
private String brand;
private String color;
private double mileage;
private int daycost;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
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 double getMileage() {
return mileage;
}
public void setMileage(double mileage) {
this.mileage = mileage;
}
public int getDaycost() {
return daycost;
}
public void setDaycost(int daycost) {
this.daycost = daycost;
}
public MotoVehicle(int no, String brand, String color, double mileage) {
super();
this.no = no;
this.brand = brand;
this.color = color;
this.mileage = mileage;
}
public MotoVehicle(int no, String brand, String color, double mileage, int daycost) {
super();
this.no = no;
this.brand = brand;
this.color = color;
this.mileage = mileage;
this.daycost = daycost;
}
public MotoVehicle(int no) {
super();
this.no = no;
}
public MotoVehicle() {
super();
}
public int CalcRent(int days){
return days*this.daycost;
}
}
4.Test
package work_multi.d3;
public class Test {
public static void main(String[] args) {
Car c=new Car(1, "as");
c.setDaycost(500);
System.out.println(c.CalcRent(3));
Bus b=new Bus(2, 5);
b.setDaycost(300);
System.out.println(b.CalcRent(9));
MotoVehicle[] m=new MotoVehicle[4];
m[0]=new Car(01, "bmw");
m[1]=new Car(02, "bmw");
m[2]=new Car(03, "别克");
m[3]=new Bus(04, 34);
for (int i = 0; i < m.length; i++) {
if(m[i] instanceof Car){
Car c1=(Car)m[i];
if(c1.getType().equals("bmw")){
m[i].setDaycost(600);
}else if(c1.getType().equals("别克")){
c1.setDaycost(500);
}else if(c1.getType().equals("别克大道")){
c1.setDaycost(300);
}
}else if(m[i] instanceof Bus){
Bus a=(Bus)m[i];
if(a.getSeatCount()<=16){
m[i].setDaycost(800);
}else{
a.setSeatCount(1500);
}
}
}
double rent=0;
for (int i = 0; i < m.length; i++) {
rent+=m[i].CalcRent(5);
}
System.out.println("租金:"+rent);
Truck t=new Truck(3, 20);
System.out.println(t.CalcRent(3));
}
}
5.Truck
package work_multi.d3;
public class Truck extends MotoVehicle{
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Truck(int no, int weight) {
super(no);
this.weight = weight;
}
public Truck(int no, String brand, String color, double mileage) {
super(no, brand, color, mileage);
}
public int CalcRent(int days){
return days*weight*50;
}
}