码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象---多态

时间:2020-05-09 20:44:07      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:override   color   and   ext   for   alc   src   ice   转型   

一、什么是多态?

   同样的物质,在不同的条件下,所呈现出来状态是不同的。

//类名  对象名  = new   类名();
  Dog  d = new Dog();
  

  Penguin  p = new Penguin();

 

二、使用

    父类的引用指向子类的实例

 

     语法:1、父类名       父类引用  =new   子类名();

 

三、向上转型

    父类的引用指向子类对象,自动进行类型转换

   <父类型>  <引用变量名>=new <子类型>();

//语法
Pet  dog=new Dog();

四、向下转型

   将一个指向子类对象的父类引用赋给一个子类的引用,即:父类类型转换为子类类型,需要强制转换。

  <子类型> <引用变量名>=(<子类型>) <父类的引用变量>;

//语法
Dog dog=(Dog)pet

 

五、编写一个汽车租赁系统

  1、编写父类汽车类,抽象方法为计算租金。

//汽车类
public abstract class MotoVehicle {
    //车牌号   品牌  日租金
private  String no;
private   String brand;
private   int perRent;


//构造方法:车牌号   品牌  日租金

public MotoVehicle(String no, String brand, int perRent) {
    this.no = no;
    this.brand = brand;
    this.perRent = perRent;
}                
//set/get方法
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 getPerRent() {
    return perRent;
}

public void setPerRent(int perRent) {
    this.perRent = perRent;
}




//抽象方法:计算租金
public abstract float calcRent(int days);
}

 2、子类:小轿车类

//轿车类
public  class Car extends MotoVehicle{
    private String type;
    //构造方法:车牌号   品牌  日租金  类型
    public Car(String no, String brand, int perRent,String type) {
    super(no,brand,perRent);
    this.type=type;
    }
    //set/get方法
    public String getType() {
        return type;
    }

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

    @Override
    public float calcRent(int days) {
        float price=this.getPerRent()*days;
        if(days>7&&days<=30){          //大于7天小于30天9折
            price*=0.9f;
        }else if(days>30&&days<=150){
            price*=0.8f;
        }else if(days>150){
            price*=0.7f;
        }
        return price;
    }
    
}

 3.子类:Bus类

public class bus extends MotoVehicle {
    //私有属性座位
private int seatCount;
//构造方法:车牌号   品牌  日租金  座位
public bus(String no, String brand, int perRent,int seatCount){
    super(no,brand,perRent);
    this.seatCount=seatCount;
}

//set/get方法
public int getSeatCount() {
    return seatCount;
}

public void setSeatCount(int seatCount) {
    this.seatCount = seatCount;
}
@Override
public float calcRent(int days) {
    float price=this.getPerRent()*days;
    if(days>3&&days<=7){
        price*=0.9f;
    }else if(days>=7&&days<30){
        price*=0.8f;
    }else if(days>=30&&days<=150){
        price*=0.7f;
    }else if(days>=150){
        price*=0.6f;
    }
    return price;
}

}

  4、汽车业务类

public class VehicleOperation {
    //汽车业务类
MotoVehicle []motoVehicle=new MotoVehicle[5];
//汽车数组
//初始化几个汽车信息
public void show(){
    motoVehicle[0]=new Car("苏A6666","宝马",800,"520i");
    motoVehicle[1]=new Car("苏A1111","宝马",500,"x6");
    motoVehicle[2]=new Car("苏A2222","别克",600,"林荫大道");
    motoVehicle[3]=new Car("苏A8888","宝马",710,"GL8");
    motoVehicle[4]=new bus("苏A9999","金龙",800,24);
}

//父类类型作为返回值
//租车   
//参数   品牌   座位数   型号    
public MotoVehicle rentMontoVehicle(String brand,int seatCount,String type){
    MotoVehicle v= null;
    //循环是小汽车还是大巴车
    for(MotoVehicle motovehicle:motoVehicle){
        if( motovehicle instanceof Car){
            Car car=(Car) motovehicle;  //向下转型
            //轿车品牌和型号与用户想要的品牌信号吻合
            if(car.getBrand().equals(brand)&&car.getType().equals(type)){
                v=car;
                break;
            }
        }else{
            bus b=(bus)motovehicle;
            if(b.getBrand().equals(brand)&&b.getSeatCount()==seatCount){
                v=b;
                break;
            }
            
        }
    }
    return v;
    
}
}

 5、测试类

//汽车租赁系统测试类
public class TestRent {
public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    
    System.out.println("*********欢迎来到汽车租赁公司!**********");
    System.out.print("请选择亲您需要租赁的车型:1、轿车  2、客车");
    int vehicleType=input.nextInt();
    //获取用户租赁汽车的三个条件;:品牌    座位数   型号
    String brand="";
    int seatCount=0;
    String type="";
    switch( vehicleType){
    case 1:
        System.out.print("请选择您要租赁的汽车品牌: 1、别克    2、宝马");
        int choose =input.nextInt();
        if(choose==1){
            brand="别克";
            System.out.print("请选择您需要租赁的类型:1、林荫大道   2、GL8");
            type=(input.nextInt()==1)?"林荫大道":"GL8";
        }else{
            
            brand="宝马";
            System.out.print("请选择您需要租赁的类型:1、x6   2、520i");
            type=(input.nextInt()==1)?"x6":"520i";
        }
        break;
    case 2:
        System.out.println("请选择您要租赁的汽车品牌:1、金杯    2、金龙");
        brand =(input.nextInt()==1)?"金杯":"金龙";
        System.out.println("请选择需要租赁的座位数:");
        seatCount=(input.nextInt()==1)?16:34;
                break;
    }
    
    VehicleOperation vehicleOper=new     VehicleOperation();
    vehicleOper.show();
    MotoVehicle m=vehicleOper.rentMontoVehicle(brand, seatCount, type);
    System.out.println("请输入您需要租赁的天数:");
    int days=input.nextInt();
    float price=m.calcRent(days);
    System.out.println("您的汽车车牌号为:"+m.getNo());
    System.out.println("您需要支付租赁费用:"+price+"元");
   
}
}

6、实现效果

技术图片

 

面向对象---多态

标签:override   color   and   ext   for   alc   src   ice   转型   

原文地址:https://www.cnblogs.com/fbwa/p/12859823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!