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 }