您的位置:首页 > 编程语言 > Java开发

JAVA 十七 抽象 常量 abstract,final

2018-02-28 11:47 239 查看

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 {<
a5b0
/span>
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java