yihuanxuri 2015-09-28 14:17 采纳率: 100%
浏览 1558

初学Java 有个地方看不懂

/*

  • 汽车类
    /
    public abstract class MotoVehicle {
    /

    • 将成员变量封装隐藏起来,private 权限只在类内显示 / private String no;//车牌号 private String brand;//品牌 private int perRend;//日租金 public abstract float calRent(int days);//抽象方法 用来计算租金 public abstract void leaseoutFlow();//租车流程 /
    • 使用set get方法实现对成员变量的存取,因为成员变量是private修饰的,只能在类内显示 */ public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getPerRend() { return perRend; } public void setPerRend(int perRend) { this.perRend = perRend; } public MotoVehicle(){

    }
    public MotoVehicle(String no,String brand,int perRend){
    this.no=no;
    this.brand=brand;
    this.perRend=perRend;
    }

}
/*

  • 桥车类
    */
    public class Car extends MotoVehicle{
    private String type;//型号

    public String getType() {
    return type;
    }

    public void setType(String type) {
    this.type = type;
    }
    public Car(){

    }

    public Car(String no,String brand,int perRent,String type){
    super(no,brand,perRent);//调用父类的构造函数
    this.type=type;
    }
    /*

    • 重写父类 calRent方法 */ public float calRent(int days){ return this.getPerRend()*days; }

    /*

    • 重写leaseoutFlow / public void leaseoutFlow(){ this.setPerRend(600); this.setNo("京60006000"); } } /
  • 客车类
    */
    public class Bus extends MotoVehicle{
    private int seatCount;//座位数

    public int getSeatCount() {
    return seatCount;
    }

    public void setSeatCount(int seatCount) {
    this.seatCount = seatCount;
    }
    public Bus(){

    }
    public Bus(String no,String brand,int perRent,int seatCount){
    super(no,brand,perRent);//调用父类的构造函数
    this.seatCount=seatCount;
    }
    /*

    • 重写父类 calRent方法 / public float calRent(int days){ return this.getPerRend()*days; } /
    • 重写leaseoutFlow */ public void leaseoutFlow(){ this.setPerRend(800); this.setNo("京80008000"); } }

/*

  • 汽车业务类 / public class Motooperation { public MotoVehicle MotoleaseOut(String MotoType){ MotoVehicle moto=null;; if(MotoType.equals("桥车")){ moto=new Car();//多态转型 类型提升 向上转型 把子类提升为父类 moto.leaseoutFlow();
    }else if(MotoType.equals("客车")){ moto=new Bus();//多态转型 类型提升 向上转型 把子类提升为父类 moto.leaseoutFlow(); } return moto; } } /
  • 汽车租赁管理类 */ import java.util.Scanner; public class RentMgrSys { public static void main(String[] args){ Scanner input=new Scanner(System.in); Motooperation motoMrs=new Motooperation(); MotoVehicle moto=null; System.out.println("欢迎来到汽车租赁公司!"); System.out.print("请输入要租赁的汽车类型: "); System.out.print("1.桥车 2.客车"); int choose=input.nextInt();//汽车类型 String MotoType=null; if(choose==1){ MotoType="桥车"; }else if(choose==2){ MotoType="客车"; } moto=motoMrs.MotoleaseOut(MotoType);//获取租赁的汽车类型 System.out.print("请输入要租赁的天数"); int days=input.nextInt(); float money=moto.calRent(days);//租赁费用 System.out.println("分配给你的汽车牌号是:"+moto.getNo()); System.out.println("你需要支付的费用:"+money+"元"); input.close(); } }
  • 写回答

5条回答 默认 最新

  • yihuanxuri 2015-09-28 14:19
    关注

    问题忘了写 float money=moto.calRent(days);这句看不懂 moto是父类MotoVehicle的对象 为什么调用的是子类的calRent()方法

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器