public class Shirt{ ....omit................ //constructors Shirt(){} Shirt(char colorCode){ setColorCode(colorCode); } Shirt(int shirtID, char colorCode){ this(colorCode); setShirtID(shirtID); } ....omit......... }
執行建構式的方式:
public class Demo{ public static void main(String[] args){ Shirt myShirt = new Shirt('G'); System.out.println("ColorCode: " + myShirt.getColorCode()); } }