物件導向-標準的手機類代碼及其測試

來源:互聯網
上載者:User

標籤: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 }

 

物件導向-標準的手機類代碼及其測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.