定义一个名为Vehicles(交通工具)的基类,该类中应包含String类型的成员属性brand(商标)和color(颜色),还应包含成员方法run(行驶,在控制台显示“我已经开动了”)和showInfo(显示信息,在控制台显示商标和颜色),并编写构造方法初始化其成员属性。 编写Car(小汽车)类继承于Vehicles类,增加int型成员属性seats(座位),还应增加成员方法showCar(在控


 1 public class Vehicles {
 2     String brand;
 3     String color;
 4 public Vehicles(String brand,String color){
 5     this.brand=brand;
 6     this.color=color;
 7 }
 8 public void run(){
 9     System.out.println("我已经开动了");
10 }
11 public void showInfo(){
12      System.out.println("商标: "  +  brand);
13      System.out.println("颜色: "  +  color);
14 }
15 
16 
17 
18 
19 }
20 --------------------------------------------------------------------------
21 public class Car extends Vehicles{
22     public Car c;
23     private int seats;
24     public Car(String brand, String color,int seats) {
25         super(brand, color);
26         this.seats=seats;
27         // TODO Auto-generated constructor stub
28     }
29 public void showCar(){
30     System.out.println("座位: "  + seats + " 个");
31 }
32 }
33 --------------------------------------------------------------------------
34 public class Truck extends Vehicles{
35     private float load;
36     public Truck(String brand, String color,float load) {
37         
38         super(brand, color);
39         this.load=load;
40         // TODO Auto-generated constructor stub
41     }
42     public void showTruck(){
43         super.showInfo();
44         System.out.println("载重"+load+"吨");
45     }
46 }
47 --------------------------------------------------------------------------
48 public class Test {
49 
50     public static void main(String[] args) {
51         // TODO Auto-generated method stub
52         Vehicles v=new Vehicles("奥迪", "黑色");
53         v.showInfo();
54         
55         Car c= new Car("大众", "红色", 6);
56         c.showCar();
57         
58         Truck truck = new Truck("解放", "蓝色", 10);
59           truck.showTruck();
60     }
61 
62     
63 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM