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

24.编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

时间:2016-09-20 23:40:24      阅读:548      评论:0      收藏:0      [点我收藏+]

标签:

package zhongqiuzuoye;

public class Car {

     String brand;
     
     public void drive()
     {}
    
    
}
package zhongqiuzuoye;

public class Aodi extends Car{
    
    public double price;
    public String model;
    public double speed;
    
    public Aodi(double price,String model,double speed)
    {
        this.price=price;
        this.model=model;
        this.speed=speed;
        System.out.println("价格为:"+price+"\t型号为:"+model+"\t初始速度为:"+speed);
    }
    
    double setSpeed(double s)
    {
        speed+=s;
        System.out.println("当前车速为:"+speed);
        return speed;
    }
}
package zhongqiuzuoye;

public class Benchi extends Car{
    
    public double price;
    public String model;
    public double speed;
    
    public Benchi(double price,String model,double speed)
    {
        this.price=price;
        this.model=model;
        this.speed=speed;
        System.out.println("价格为:"+price+"\t型号为:"+model+"\t初始速度为:"+speed);
    }
    
    double setSpeed(double s)
    {
        speed+=s;
        System.out.println("当前车速为:"+speed);
        return speed;
    }

}
package zhongqiuzuoye;

public class E {
    public static void main(String[] args) {

    Aodi a4=new Aodi(300000,"A4",40);
    a4.setSpeed(-10);
    
    Benchi e200=new Benchi(500000,"e200",50);
    e200.setSpeed(50);
    
}
}

技术分享

 

24.编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

标签:

原文地址:http://www.cnblogs.com/zs6666/p/5890925.html

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