面向?qū)ο?--初體驗(類,方法,對象,構(gòu)造函數(shù))

1.類,方法,對象,構(gòu)造函數(shù)的例題(1)


public class Account {

//private 只在本類中運(yùn)行。

//public在所有的工程中都可以調(diào)用。

//protected只在本類本包和子類中調(diào)用。? ? //實例方法如果是public類型則所有可運(yùn)行。

//default在本來和子類中被調(diào)用。

//get有返回值但是沒有參數(shù),set沒有返回值但是有參數(shù)。

private String id;

private String name;

private int balance = 0;

public Account(String id, String name) {

this.id = id;

this.name = name;

}

public Account(String id, String name, int balance) {

this.id = id;

this.name = name;

this.balance = balance;

}

public String getID() {

return id;

}

public String getName() {

return name;

}

public int getbalance() {

return balance;

}

public int credit(int amount) {

balance=balance+amount;

return balance;

}

public int debit(int amount) {

if (amount <= balance) {

balance=balance -amount;

} else {

System.out.println("Amount exceeded balance");

}

return balance;

}

public int transferto(Account another, int amount) {

if (amount <= balance) {

balance =balance-amount;

another.credit(amount);

} else {

System.out.println("Amount exceeded balance");

}

return balance;

}

public String toString() {

return "Account=[id=" + id + ",name=" + name + ",balance=" + balance + ",]";

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Account ac = new Account("2014", "xy",5000); // new是一個關(guān)鍵字,new 的時候就調(diào)用了構(gòu)造函數(shù)

ac.credit(5000);

System.out.println(ac.toString()); // 調(diào)用函數(shù)時,格式要一致??凑{(diào)用的方法是否有參數(shù),有則需傳入對應(yīng)的參數(shù)類型。

}//Account=[id=2014,name=xy,balance=10000,]

}

1.類,方法,對象,構(gòu)造函數(shù)的例題(2)//類的包含。

public class Book{

private String name;

private Author author;

private double price;

private int qty=0;

public Book(String name,Author author,double price){

this.name=name;

this.author=author;?//通過構(gòu)造函數(shù)構(gòu)造了一個實例對象,故在后面再本來中可以直接調(diào)用

this.price=price;

}

public Book(String name,Author author,double price,int qty){

this.name=name;

this.author=author;

this.qty=qty;

}

public String getName(){

return name;

}

public Author getAuthor(){

return author;

}

public double getPrice(){

return qty;

}

public void setPrice(double price){

this.price=price;

}

public int getQty(){

return qty;

}

public void setQty(int qty){

this.qty=qty;

}

public String toString(){

String i="Book{name="+name+",email="+author.getEmail()+",gender="+author.getGender()+",price="+price+",qty="+qty+"]";

return i;

}

}


public class Book{

private String name;

private Author author;

private double price;

private int qty=0;

public Book(String name,Author author,double price){

this.name=name;

this.author=author;

this.price=price;

}

public Book(String name,Author author,double price,int qty){

this.name=name;

this.author=author;

this.qty=qty;

}

public String getName(){

return name;

}

public Author getAuthor(){

return author;

}

public double getPrice(){

return qty;

}

public void setPrice(double price){

this.price=price;

}

public int getQty(){

return qty;

}

public void setQty(int qty){

this.qty=qty;

}

public String toString(){

String i="Book{name="+name+",email="+author.getEmail()+",gender="+author.getGender()+",price="+price+",qty="+qty+"]";

return i;}}


public class Book_Test {

public static void main(String[] args) {

Author au = new Author(); //在輸出之前需要實例化Author,才能調(diào)用。

Book bk = new Book("xiaoyang",au,2000);

System.out.println(bk.toString());}}


1.類,方法,對象,構(gòu)造函數(shù)的例題(3)--將author變成數(shù)組。

authors:Author[];

Book[name=xy,author={Author[name=聰哥,email=1@qq.com,gender=女],Author[name=周嘉宇,email=113@qq.com,gender=男]},price=2000.0,qty=0] //輸出結(jié)果

Book[name=xy,author={},price=2000.0,qty=0] //book.toString()中輸出的內(nèi)容。

Author[name=周嘉宇,email=113@qq.com,gender=男] //Author.toString()中輸出的內(nèi)容。


main 方法中:

Author[] au = {new Author("李聰聰","1015612131@qq.com",'女'),new Author("周嘉宇","11351313@qq.com",'男')}; ——>Author[] au = {new Book(), new Author() };

Book bk = new Book("xiaoyang",au,2000);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,922評論 0 33
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,119評論 0 23
  • 人工智能,一個越來越火熱的名詞,包括但不限于BAT的互聯(lián)網(wǎng)巨頭都對其趨之若鶩。大量資金的注入,人才的培養(yǎng),戰(zhàn)略的制...
    empror閱讀 344評論 0 0
  • 一早就收到了大姑子送的康乃馨,幸福! 十點(diǎn)多兒子發(fā)信息祝母親節(jié)快樂! 用布衣老師教的心法抄一遍對話內(nèi)容。...
    墨蘭閱讀 186評論 1 1

友情鏈接更多精彩內(nèi)容