用集合做参数:先在main方法中,创建book对象,放进接口之后,接口作为参数进行传递,对person对象进行初始化.
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对象的.
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;
/**
* 集合 本身也是一个对象
* 可以作为 属性 参数 实际参数 传递
* debug
* 1 先确定要 检测的部分, 添加断点
* 2 用debug模式进行 运行
*
* Ctrl + shif + i 得到选中的语句的 结果
*
* F5 进入指定的方法
* F6 下一行
* F8 下一个断点
*/
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从入门到放弃", 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");
}
}