用集合做參數(shù):先在main方法中,創(chuàng)建book對(duì)象,放進(jìn)接口之后,接口作為參數(shù)進(jìn)行傳遞,對(duì)person對(duì)象進(jìn)行初始化.
Book類:
package com.qf.demo4;
public class Book {
private String bookName;
private double price;
private String author;
public Book(String bookName, double price, String author) {
super();
this.bookName = bookName;
this.price = price;
this.author = author;
}
public Book() {
super();
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book [bookName=" + bookName + ", price=" + price + ", author=" + author + "]";
}
}
Person類
注意:類的屬性定義為接口,如何初始化person對(duì)象的.
package com.qf.demo4;
import java.util.ArrayList;
public class Person {
private String name;
private int age;
private ArrayList<Book> books;
public Person(String name, int age, ArrayList<Book> books) {
super();
this.name = name;
this.age = age;
this.books = books;
}
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ArrayList<Book> getBooks() {
return books;
}
public void setBooks(ArrayList<Book> books) {
this.books = books;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", books=" + books + "]";
}
}
main方法:
package com.qf.demo4;
/**
* 集合 本身也是一個(gè)對(duì)象
* 可以作為 屬性 參數(shù) 實(shí)際參數(shù) 傳遞
* debug
* 1 先確定要 檢測(cè)的部分, 添加斷點(diǎn)
* 2 用debug模式進(jìn)行 運(yùn)行
*
* Ctrl + shif + i 得到選中的語(yǔ)句的 結(jié)果
*
* F5 進(jìn)入指定的方法
* F6 下一行
* F8 下一個(gè)斷點(diǎn)
*/
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Book> books = new ArrayList<>();
books.add(new Book("java編程思想", 108, "二狗"));
books.add(new Book("java從入門(mén)到放棄", 1008, "康帥傅"));
Person person =new Person("小香菇", 18, books);
System.out.println(person);
Person person2 = new Person();
System.out.println(person2);
person2.setName("皮皮蝦");
person2.setAge(18);
System.out.println(person2);
// 必須 給 books 集合 初始化
person2.setBooks(books);
String author = person2.getBooks().get(0).getAuthor();
System.out.println(author);
int w = 5;
int i = w+7+w+4+7;
}
public static void test(){
System.out.println("abc");
}
}