继承—Car

 1 package jicheng;
 2 
 3 public class Car {
 4     /*
 5      * 编写一个Car类,具有final类型的属性品牌,具有功能drive; 
 6      * 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;
 7      * 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。
 8      */
 9     final String pinpai;
10     
11     public Car(String pinpai) {
12         super();
13         this.pinpai = pinpai;
14     }
15 
16     public void drive()
17     {
18         System.out.println("开着奥迪和奔驰");
19     }
20 }
 1 package jicheng;
 2 
 3 public class Aodi extends Car {
 4     
 5     /*
 6      * 编写一个Car类,具有final类型的属性品牌,具有功能drive;
 7      *  定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;
 8      * 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。
 9      */
10     public Aodi(String model) {
11         super("Aodi");
12         this.model = model;
13     }
14         
15     
16 //    public void Aod(int price, String model)
17 //    {
18 //        this.Price=price;
19 //        this.model=model;
20 //    }
21     private int Price;
22     
23     public int getPrice() {
24         return Price;
25     }
26     public void setPrice(int price) {
27         Price = price;
28     }
29     public String getModel() {
30         return model;
31     }
32     public void setModel(String model) {
33         this.model = model;
34     }
35     private String model;
36     public void Biansu(int Sudu)
37     {
38         System.out.println("速度为:"+Sudu);
39     }
40 }
 1 package jicheng;
 2 
 3 public class Benchi extends Car {
 4 
 5 
 6     /*
 7      * 编写一个Car类,具有final类型的属性品牌,具有功能drive;
 8      *  定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;
 9      * 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。
10      */
11     
12     private int Price;
13     private String model;
14     
15     public Benchi(String model) {
16         super("Benchi");
17         this.model = model;
18     }
19    
20     public int getPrice() {
21         return Price;
22     }
23 
24     public void setPrice(int price) {
25         Price = price;
26     }
27 
28     public String getModel() {
29         return model;
30     }
31 
32     public void setModel(String model) {
33         this.model = model;
34     }
35 
36     public void Biansu(int Sudu)
37     {
38         System.out.println("速度为:"+Sudu);
39     }
40 
41 }
 1 package jicheng;
 2 
 3 public class E {
 4 
 5     public static void main(String[] args) {
 6         // TODO 自动生成的方法存根
 7         Car c=new Car("Aodi");
 8         Aodi a=new Aodi("A6L");
 9         System.out.println("这款Aodi的型号为:"+a.getModel());
10         a.setPrice(350000);
11         System.out.println("Aodi-A6的价格为:"+a.getPrice());
12         a.Biansu(90);
13         Benchi b=new Benchi("GLK300");
14         System.out.println("这款Benchi的型号为:"+b.getModel());
15         b.setPrice(400000);
16         System.out.println("Benchi-GLK300的价格为:"+b.getPrice());
17         b.Biansu(100);
18     }
19 
20 }

原文地址:https://www.cnblogs.com/yg6405816/p/5522238.html