標籤:log font test this static 賦值 return -- 土豪金
1 /* 2 作業:請把手機類寫成一個標準類,然後建立對象測試功能。 3 4 手機類: 5 成員變數: 6 品牌:String brand; 7 價格:int price; 8 顏色:String color; 9 成員方法:10 針對每一個成員變數給出對應的getXxx()/setXxx()方法。11 最後定義測試:12 建立一個對象,先通過getXxx()方法輸出成員變數的值。這一次的結果是:null---0---null13 然後通過setXxx()方法給成員變數賦值。再次輸出結果。這一次的結果是:三星---2999---土豪金14 */15 class Phone {16 //品牌17 private String brand;18 //價格19 private int price;20 //顏色21 private String color;22 23 //getXxx()和setXxx()方法24 public String getBrand() {25 return brand;26 }27 28 public void setBrand(String brand) {29 this.brand = brand;30 }31 32 public int getPrice() {33 return price;34 }35 36 public void setPrice(int price) {37 this.price = price;38 }39 40 public String getColor() {41 return color;42 }43 44 public void setColor(String color) {45 this.color = color;46 }47 }48 49 class PhoneTest {50 public static void main(String[] args) {51 //建立手機對象52 Phone p = new Phone();53 54 //直接輸出預設值55 System.out.println(p.getBrand()+"---"+p.getPrice()+"---"+p.getColor());56 57 //給成員變數賦值58 p.setBrand("三星");59 p.setPrice(2999);60 p.setColor("土豪金");61 //再次輸出62 System.out.println(p.getBrand()+"---"+p.getPrice()+"---"+p.getColor());63 }64 }
物件導向-標準的手機類代碼及其測試