yihuanxuri 2015-09-28 14:35 采纳率: 100%
浏览 1893
已采纳

初学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 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 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 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();
    }
    }
    这句看不懂 float money=moto.calRent(days); moto是父类Motooperation的对象为什么调用的是子类的calRent()方法

  • 写回答

8条回答 默认 最新

  • xiedacon 2015-09-28 15:06
    关注

    moto是MotoVehicle类型的变量啊,motoMrs.MotoleaseOut(MotoType)得到的是MotoVehicle类的子类的对象。变量是在栈内存中的,对象是在堆内存中的。
    moto变量能调用的方法由MotoVehicle类型决定,而具体怎么调用还要看子类对象,如果重写了calRent()方法就用子类的,没有就去找父类。
    这是方法的重载解析。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能