[实验任务一]:旅行方式的选择
旅游的出行方式有乘坐飞机旅行、乘火车旅行和自行车游,不同的旅游方式有不同的实现过程,客户可以根据自己的需要选择一种合适的旅行方式。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
package strategy;
public class Person {
private TourStrategy ts;
public void setTs(TourStrategy ts) {
this.ts = ts;
}
public void travel(){
ts.travel();
}
}
package strategy;
public interface TourStrategy {
public void travel();
}
package strategy;
public class AirStrategy implements TourStrategy{
@Override
public void travel() {
System.out.println("乘坐飞机旅游");
}
}
package strategy;
public class TrainStrategy implements TourStrategy{
@Override
public void travel() {
System.out.println("乘坐火车旅游");
}
}
package strategy;
public class BikeStrategy implements TourStrategy{
@Override
public void travel() {
System.out.println("骑自行车旅游");
}
}
package strategy;
public class Client {
public static void main(String[] args) {
Person p = new Person();
TourStrategy air = new AirStrategy();
p.setTs(air);
p.travel();
System.out.println("-------------------------");
TourStrategy train = new TrainStrategy();
p.setTs(train);
p.travel();
System.out.println("-------------------------");
TourStrategy bike = new BikeStrategy();
p.setTs(bike);
p.travel();
}
}
3. 注意编程规范。