也是有關於java子類別該如何寫

已知父類別、主程式內容及答案列印結果如下

class Car

{

public String color;

public Car(String str)

{

color=str;

}

public void show()

{

System.out.println("車身顏色:"+color);

}

}

class Truck extends Car

{

←這裡

}

class SmallCar extends Car

{

←跟這裡

}

public class ex4

{

public static void main(String args[])

{

Car t1=new Truck("Blue","C2-5678","Ariel");

t1.show();

Car t2=new SmallCar("Red","T3-1234","John");

t2.show();

}

}

--------------------------------------------------------------------

卡車車主姓名 卡車車牌號碼 卡車車身顏色

Ariel C2-5678 Blue

小車車主姓名 小車車牌號碼 小車車身顏色

John T3-1234 Red

--------------------------------------------------------------------

也是請問各位大大中間缺少的子類別怎麼寫....?

拜託各位了

3 Answers

Rating
  • 1 decade ago
    Favorite Answer

    class Car

    {

    public String color;

    public Car(String str)

    {

    color=str;

    }

    /**

    * Car

    */

    public Car() {

    }

    public void show()

    {

    System.out.println("車身顏色:"+color);

    }

    }

    class Truck extends Car

    {

    public String CarCode;//宣告車牌號碼

    public String OwnerName;//宣告車主姓名

    public Truck() {

    }

    public Truck(String str, String code, String name) {//建構子

    color = str;

    CarCode = code;

    OwnerName = name;

    }

    public void show()

    {

    System.out.println("卡車車主姓名" + " 卡車車牌號碼" + " 卡車車身顏色" + "\n");//空格只是為了美觀

    System.out.println( OwnerName +" " +CarCode +" " + color);

    }

    }

    class SmallCar extends Car

    {

    public String CarCode;

    public String OwnerName;

    public SmallCar() {

    }

    public SmallCar(String str, String code, String name) {

    color = str;

    CarCode = code;

    OwnerName = name;

    }

    public void show()

    {

    System.out.println("小車車主姓名" + " 小車車牌號碼" + " 小車車身顏色" + "\n");

    System.out.println( OwnerName +" " +CarCode +" " + color);

    }

    }

    public class ex4

    {

    public static void main(String args[])

    {

    Car t1=new Truck("Blue","C2-5678","Ariel");

    t1.show();

    Car t2=new SmallCar("Red","T3-1234","John");

    t2.show();

    }

    }

    Source(s): 自己
  • 5 years ago

    ●九州 娛樂 網站 http://ts777.cc

    ●●●運彩遊戲、真人遊戲、電子遊戲、對戰遊戲、對戰遊戲●●●

    ●新舊會員儲值就送500點

    ● 真人百家樂彩金等你拿

    ●線上影片直播、正妹圖、討論區免費註冊

    歡迎免費體驗交流試玩!

    ●九州 娛樂 網站 http://ts777.cc

  • 1 decade ago

    javaworld

    有介紹詳細的知識,可以當作參考

Still have questions? Get your answers by asking now.