JAVA 十七 抽象 常量 abstract,final

JAVA 十七 抽象,常量 abstract,final

标签(空格分隔): JAVA


1.abstract抽象类

  • 用abstract修饰的类是抽象类
  • 如果用abstract修饰的类是无法生成对象的,即无法实例化

2.abstract抽象方法

  • 没有方法体即没有大括号{ };
  • 如果类里面包含抽象方法,那么该类必须为抽象类抽象类里面不一定有抽象方法
  • 子类继承父类,必须重写父类抽象方法,

3.final修饰属性,方法,类

  • 修饰的属性是常量
  • 修饰的方法为最终方法无法重写
  • 修饰的类为最终类,无法被继承
//汽车租聘
public abstract class MotoVehicle {
    private int no;
    private String brand,color;
    private double mileage;
    public abstract void CalcRent(int days);
    public MotoVehicle(int no,String brand,String color,double mileage){
        this.setNo(no);
        this.setBrand(brand);
        this.setColor(color);
        this.setMileage(mileage);
    }
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public double getMileage() {
        return mileage;
    }
    public void setMileage(double mileage) {
        this.mileage = mileage;
    }
}
//CAR类

public final class Car extends MotoVehicle{
    private String  type;
    private int money;
    public void CalcRent(){

    }
    public Car(int no,String brand,String color,double mileage,String type){
        super(no,brand,color,mileage);
        this.type=type;
    }
    @Override
    public void CalcRent(int days) {
        // TODO Auto-generated method stub
        if(type.equals("别克GL8")){
            money=days*600;
        }
        if(type.equals("宝马")){
            money=days*500;
        }
        if(type.equals("别克")){
            money=days*300;
        }
    }
    public void show(){
        System.out.println("车牌号:"+super.getNo()+"品牌:"+super.getBrand()+"型号:"+type+"颜色:"+super.getColor()+"公里:"+super.getMileage()+"花费:"+money);
    }

}
//BUS类

public final class Bus extends MotoVehicle{
    private String sentcount;
    private int money;
    public void CalcRent(){

    }
    public Bus(int no,String brand,String color,double mileage,String sentcount){
        super(no,brand,color,mileage);
        this.sentcount=sentcount;
    }
    @Override
    public void CalcRent(int days) {
        // TODO Auto-generated method stub
        if(sentcount.equals("金杯")){
            money=days*800;
        }
        if(sentcount.equals("金龙")){
            money=days*1500;
        }
    }
    public void show(){
        System.out.println("车牌号"+super.getNo()+"品牌"+super.getBrand()+"型号"+sentcount+"颜色"+super.getColor()+"公里"+super.getMileage()+"花费"+money);
    }
}
/测试类

public class Text {
    public static void main(String[] args){
        System.out.println("构造方法中输入\t车牌号\t型号\t公里数\t具体型号");
        Car aa=new Car(888,"轿车","绿色",2000,"别克GL8");
        aa.CalcRent(8);
        aa.show();
        Bus bb=new Bus(888,"面包车","绿色",2000,"金杯");
        aa.CalcRent(10);
        aa.show();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值