定義一個名為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