JAVA基礎學習之路(十)this關鍵字

來源:互聯網
上載者:User

標籤:構造方法   java基礎學習   java   構造   nbsp   重複   世界   關鍵字   互相調用   


class
Book { String name; int price; int num;
//構造方法之間的互相調用解決了代碼的重複問題,但是一定要留出口
public Book() { this("請輸入書名",0,0); } public Book(String name) { this(name,9999,9999);//this調用方法 } public Book(String name,int num) { this(name,num,9999); } public Book(String name,int num,int price) { this.name = name; //this調用屬性 this.price = price; this.num = num; } public String getInfo() { return "書名: "+this.name+"\n"+ "數目: "+this.num+"\n" + "單價: " +this.price; }} public class test1 { public static void main(String args[]) { Book book_0 = new Book(); Book book_1 = new Book("我的世界"); Book book_2 = new Book("老人與海",5); Book book_3 = new Book("陸炳勳",3,2); System.out.println(book_0.getInfo()); System.out.println(book_1.getInfo()); System.out.println(book_2.getInfo()); System.out.println(book_3.getInfo()); } }

 

JAVA基礎學習之路(十)this關鍵字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.