public interface Transport {
//选择的交通方式
public void traffic();
}
public class Train implements Transport{
@Override
public void traffic() {
System.out.println("您好,您选择了火车作为出行方式,我将竭诚为您服务");
}
}
public class Airplane implements Transport{
@Override
public void traffic() {
System.out.println("您好,您选择了飞机作为出行方式,我将竭诚为您服务");
}
}
public class Bus implements Transport{
@Override
public void traffic() {
System.out.println("您好,您选择了巴士作为出行方式,我将竭诚为您服务");
}
}
package com.imocc.objectproject.sample.system;
public class CustomerSrevice {
public Transport contact (int distance){
if(distance>1000){
return new Airplane();
}else if(distance<1000 &&distance >300){
return new Train();
}else {
return new Bus();
}
}
public static void main(String[] args) {
Transport transport = new Train();
transport.traffic();
Transport transport1 = new Airplane();
transport1.traffic();
Transport transport2 = new Bus();
transport2.traffic();
CustomerSrevice cs = new CustomerSrevice();
Transport transport3 =cs.contact(1200);
transport3.traffic();
}
}
public class Customer {
public static void main(String[] args) {
System.out.print("请您输入出发地到目的地的距离为:");
int distancer =new Scanner(System.in).nextInt();
CustomerSrevice customerSrevice = new CustomerSrevice();
Transport transport =customerSrevice.contact(1200);
transport.traffic();
}
}