多态的应用(2)

package day14.t1.polumorphic;

public class TestApplyPolymorphic {
   public static void main(String[] args) {
    Car car = new Car(); //自身类型引用指向自身类型
    car.type ="小汽车";
    car.speed = 120;
    car.price = 200000.00;
    car.brand = "BMW";   //私有属性
    car.run();
    
    Vehicle veh = new Car();   //父类引用指向子类对象
    veh.type="小汽车";
    veh.speed=150;
    veh.price=3000000.00;
    
    veh.run();
        
    Bus bus = new Bus();
    bus.type="公交车";
    bus.speed=40;
    bus.price = 1000000.00;
    bus.seatNum = 20;
    
    Bicyle bic = new Bicyle();
    bic.type="自行车";
    bic.speed=20;
    bic.price=200.2;
    bic.color="hongse";
    
    
    Empoyee emp = new Empoyee();
    emp.name="tom";
    //emp.goHome(bus);
    emp.goHome(bic); 
    
    //新内容
    Empoyee emp2 = new Empoyee();
    emp2.name = "jack";
    Vehicle myvehicle = emp2.buyVehicle(22);
    
    if( myvehicle != null) {
        myvehicle.run();
    }else {
        System.out.println("没钱");
    }
}
}

//员工
class Empoyee{
    String name;
    
    //回家(父类类型作为方法的形参,实现多态)
    
    public void goHome(Vehicle veh) {
        System.out.print(name + "正在回家");
        veh.run();
    }
    
    //新内容
    public Vehicle buyVehicle(int money) {
        
        Vehicle veh = null;
        
        if(money >= 100) {
            Bus bus =  new Bus();
            bus.type="公交车";
            bus.speed=50;    
            bus.price=1000000;
            bus.seatNum=16;        
            veh = bus;
        }else if(money >= 30) {
            Car car = new Car();
            car.type="小汽车";
            car.speed=80;
            car.price=305287;
            car.brand="Bezs";
            veh = car;
            
        }else if(money >=20){
            Bicyle bic = new Bicyle();
            bic.type="自行车";
            bic.speed=30;
            bic.price=2100;
            bic.color="黑色";
            veh = bic;
        }
        return veh;
    }
    
    /*
     * public void goHome(Bus bus) { System.out.print(name + "正在回家"); bus.run(); }
     * public void goHome(Bicyle bic) { System.out.print(name + "正在乘坐"); bic.run();
     * }
     */
}


class Vehicle{
    String type;
    int speed;
    double price;
    
    public void run() {
        System.out.println("一辆价值  " + price   + type +speed + "速度");
    }

class Car extends Vehicle{
    String brand;
    
    public void run() {
        System.out.println("一辆价值  " + price   + type +speed + "速度" + brand +"品牌");
    }
}
class Bus extends Vehicle{
    int seatNum; 
}
class Bicyle extends Vehicle{
    String color;
    public void run() {
        System.out.println("一辆价值  "  + price + color +   type +speed + "速度");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值