面向对象系统分析和设计综合实验报告4 联系客服

发布时间 : 星期一 文章面向对象系统分析和设计综合实验报告4更新完毕开始阅读

西南科技大学计算机学院 《面向对象系统分析和设计》实验报告 //注入一个折扣类对象

public void setDiscount(Discount discount) { this.discount = discount; }

public double getPrice() {

//调用折扣类的折扣价计算方法

return discount.calculate(this.price); } }

//VIP会员票折扣类:具体策略类

public class VIPDiscount implements Discount { public double calculate(double price) { System.out.println(\票:\); System.out.println(\增加积分!\); return price * 0.5; } }

//学生票折扣类:具体策略类

public class StudentDiscount implements Discount { public double calculate(double price) { System.out.println(\学生票:\); return price * 0.8; } }

//儿童票折扣类:具体策略类

public class ChildrenDiscount implements Discount { public double calculate(double price) { System.out.println(\儿童票:\); return price - 10; } }

public class MoviceClient {

public static void main(String args[]) { MovieTicket mt = new MovieTicket(); double originalPrice = 60.0; double currentPrice;

mt.setPrice(originalPrice);

System.out.println(\原始价为:\ + originalPrice);

System.out.println(\);

5

西南科技大学计算机学院 《面向对象系统分析和设计》实验报告 }

Discount discount =new VIPDiscount(); //vip 用户 mt.setDiscount(discount); //注入折扣对象 currentPrice = mt.getPrice();

System.out.println(\折后价为:\ + currentPrice); discount =new StudentDiscount(); //学生用户 mt.setDiscount(discount); //注入折扣对象 currentPrice = mt.getPrice();

System.out.println(\折后价为:\ + currentPrice); discount =new ChildrenDiscount(); //儿童用户 mt.setDiscount(discount); //注入折扣对象 currentPrice = mt.getPrice();

System.out.println(\折后价为:\ + currentPrice); }

3) 实现结果:

4. 某软件公司欲开发一款飞机模拟系统,该系统主要模拟不同种类飞机的飞行特征与起飞特征,需要模拟的飞机种类及其特征如表1所示:

表1 飞机种类及特征一览表

飞机种类 直升机(Helicopter) 客机(AirPlane) 歼击机(Fighter) 鹞式战斗机(Harrier) 起飞特征 垂直起飞(VerticalTakeOff) 长距离起飞(LongDistanceTakeOff) 长距离起飞(LongDistanceTakeOff) 垂直起飞(VerticalTakeOff) 飞行特征 亚音速飞行(SubSonicFly) 亚音速飞行(SubSonicFly) 超音速飞行(SuperSonicFly) 超音速飞行(SuperSonicFly) 为将来能够模拟更多种类的飞机,试采用策略模式设计并模拟实现该飞机模拟系统。 1) 类图

6

西南科技大学计算机学院 《面向对象系统分析和设计》实验报告

2) 实现代码:

public class plane {

private state state;//状态

public void settakeoffFeatures(state tFeatures){ this.state = tFeatures; }

public void setplanetype(String type) {

if(type==\直升机\)

state=new Helicopter(); else if(type==\客机\){ state=new AirPlane(); }

else if(type==\歼击机\){ state=new Fighter(); }

else if(type==\鹞式战斗机\){ state=new Harrier(); }

else{

state=null; } }

public void takeoff(){ state.takeOff(); }

public void fly(){ state.fly(); } }

7

西南科技大学计算机学院 《面向对象系统分析和设计》实验报告 public class AirPlane implements state{ @Override

public String takeOff() {

System.out.println(\长距离起飞\); return \长距离起飞\; }

@Override

public String fly() {

System.out.println(\亚音速飞行\); return \亚音速飞行\; } }

public class Fighter implements state{ @Override

public String takeOff() {

System.out.println(\长距离起飞\); return \长距离起飞\; }

@Override

public String fly() {

System.out.println(\超亚音速飞行\); return \超音速飞行\; } }

public class Harrier implements state{ @Override

public String takeOff() {

System.out.println(\垂直起飞\); return \垂直起飞\; }

@Override

public String fly() {

System.out.println(\超亚音速飞行\); return \超音速飞行\; }

}

public class Helicopter implements state{ @Override

public String takeOff() {

System.out.println(\垂直起飞\); return \垂直起飞\; }

8