首页 > 代码库 > 作业2

作业2

编写一个Car类,具有String类型的属性品牌,具有功能drive;

定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;

定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特

性。

package Car;public class Car {    private String pinpai;                   private double sudu;        public String getPinpa() {        return pinpai;    }    public void setPinpa(String pinpa) {        this.pinpai = pinpa;    }    public double getSudu() {        return sudu;    }    public void setSudu(double sudu) {        this.sudu = sudu;    }    public String drive(){        return "汽车已启动!";    }    }  package Car;public class Aodi extends Car{    private double jiage;    private String xinghao;        public double getJiage() {        return jiage;    }    public void setJiage(double jiage) {        this.jiage = jiage;    }    public String getXinghao() {        return xinghao;    }    public void setXinghao(String xinghao) {        this.xinghao = xinghao;    }    public double biansu(){        double sudu=super.getSudu()+10;        return sudu;    }}package Car;public class Benchi extends Car{    private double jiage;    private String xinghao;        public double getJiage() {        return jiage;    }    public void setJiage(double jiage) {        this.jiage = jiage;    }    public String getXinghao() {        return xinghao;    }    public void setXinghao(String xinghao) {        this.xinghao = xinghao;    }    public double biansu(){        double sudu=super.getSudu()+10;        return sudu;    }}package Car;public class E {    public static void main(String[] args){        Aodi aodi=new Aodi();        aodi.setJiage(666666);        aodi.setXinghao("奥迪A6");        aodi.setSudu(50);        System.out.println("奥迪车的型号是:"+aodi.getXinghao()+" 价格是:"+aodi.getJiage());        System.out.println(aodi.drive());        System.out.println("启动后的速度为:"+aodi.biansu());        Benchi benchi=new Benchi();        benchi.setJiage(888888);        benchi.setXinghao("奔驰S600");        benchi.setSudu(45);        System.out.println("奔驰车的型号是:"+benchi.getXinghao()+" 价格是:"+benchi.getJiage());        System.out.println(benchi.drive());        System.out.println("启动后的速度为:"+benchi.biansu());            }}

技术分享

作业2