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);