慕仙子
2021-02-10 11:59
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(); } }
看你的代码是把键盘输入赋值给distancer,那么你在调用CustomerSrevice类的实例化对象customerSrevice中的conta方法时,只需要将传入参数1200换成distancer即可。除此之外,CustomerService类中其实并不需要main方法,你的main应该也是跟着课程做的尝试,使用时并不需要。
Java入门第二季 升级版
530553 学习 · 6091 问题
相似问题