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

package com.hanqi.test;

public class Car {
    //构造一个汽车的属性
private String name;
//创建getter和setter方法
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public void drive()
    {
        
    }

}
package com.hanqi.test;

public class Aodi extends Car {
    public double price;//定义私有属性价格和型号
    
    public String style;
    //构造方法
    Aodi(double price,String style)
    {
        this.price=price;
        this.style=style;
        
    }
    
    //成员方法
    public void Speed(double aos )
    {
        System.out.println("速度是:"+aos);
    }
    

}
package com.hanqi.test;

public class Benchi extends Car {
    
    public double price;
    
    public String style;
    
    //构造方法
        Benchi(double price,String style)
        {
            this.price=price;
            this.style=style;
            
        }
    //成员方法
    public void Speed(double bensp)
    {
        System.out.println("速度是:"+bensp);
    }
    
    

}
package com.hanqi.test;

public class Test1 {

    public static void main(String[] args) {

   Aodi ad=new Aodi(350000,"A4L");
   ad.setName("奥迪");
   System.out.println("Aodi的name: "+ad.getName()+",型号是:"+ad.style+",价格是:"+ad.price);
   ad.Speed(120);
   Benchi bc=new Benchi(400000,"s300L");
   bc.setName("奔驰");
   System.out.println("benchi的name:"+bc.getName()+",型号是;"+bc.style+",价格是:"+bc.price);
   bc.Speed(150);

    }

}

 

posted @ 2016-09-20 21:26  -加勒比海带  阅读(1222)  评论(0编辑  收藏  举报