继承—Car

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

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

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

 1 public class Car {
 2     private final String PinPai;
 3     
 4     public String getPinPai(){
 5         return PinPai;
 6     }
 7     public void drive(){
 8         System.out.println("我可以行驶。");
 9     }
10     
11     public Car(String pinpai){
12         PinPai=pinpai;
13     }
14     
15 }
 1 public class Aodi extends Car {
 2 
 3     public double price;
 4     public String xinghao;
 5 
 6     public void biansu() {
 7         System.out.println("速度改变");
 8     }
 9 
10     public Aodi() {
11         super("奥迪");
12     }
13 
14     public void Aodi(double price, String xinghao) {
15         this.price = price;
16         this.xinghao = xinghao;
17     }
18 
19     @Override
20     public String toString() {
21         return "Aodi [PinPai=" + getPinPai() + ", price=" + price + ", xinghao=" + xinghao + "]";
22     }
23 
24 }
 1 public class BenChi extends Car{
 2 
 3     public double price;
 4     public String xinghao;
 5     
 6     public void biansu(){
 7         System.out.println("速度改变");
 8     }
 9     
10     public BenChi(){
11         super("奔驰");
12     }
13     public void BenChi(double price, String xinghao){
14         this.price=price;
15         this.xinghao=xinghao;
16     }
17 
18     @Override
19     public String toString() {
20         return "BenChi [PinPai=" + getPinPai() + ", price=" + price + ", xinghao=" + xinghao + "]";
21     }
22     
23 }
 1     public static void main(String[] args) {
 2         Aodi ad = new Aodi();
 3         ad.Aodi(400000, "A6");
 4         ad.biansu();
 5         System.out.println(ad.toString());
 6 
 7         BenChi bc = new BenChi();
 8         System.out.println(bc.getPinPai());
 9         bc.BenChi(350000, "Benze S—500");
10         System.out.println(bc.toString());
11 
12     }

运行结果:

posted @ 2016-05-23 14:05  唐枫  阅读(274)  评论(0编辑  收藏  举报