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

作业2

时间:2016-09-21 21:40:09      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

编写一个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

标签:

原文地址:http://www.cnblogs.com/wangguoning/p/5893990.html

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