返回列表 發帖
  1. public class ch58
  2. {
  3.     public static void main(String args[])
  4.     {
  5.          Car bus=new Car("公車",6,40);
  6.          Car truck=new Car(8,3);
  7.          truck.name="卡車";
  8.          Car taxi=new Car("計程車",4,5);
  9.          System.out.println(bus.name+"有"+bus.wheel+"個輪子,可載"+bus.carry+"人.");
  10.          System.out.println(truck.name+"有"+truck.wheel+"個輪子,可載"+truck.carry+"人.");
  11.          System.out.println(taxi.name+"有"+taxi.wheel+"個輪子,可載"+taxi.carry+"人.");
  12.     }
  13. }
  14. class Car
  15. {
  16.     String name;
  17.     int wheel;
  18.     int carry;
  19.     Car()
  20.     {
  21.     }
  22.     Car(String name)
  23.     {
  24.         this.name=name;
  25.     }
  26.     Car(int wheel, int carry)
  27.     {
  28.         this.wheel=wheel;
  29.         this.carry=carry;
  30.     }
  31.     Car(String name, int wheel, int carry)
  32.     {
  33.         this.name=name;
  34.         this.wheel=wheel;
  35.         this.carry=carry;
  36.     }
  37. }
複製代碼

TOP

返回列表