class Book {
String name;
int price;
int num;
//构造方法之间的互相调用解决了代码的重复问题,但是一定要留出口
public Book() {
this("请输入书名",,);
}
public Book(String name) {
this(name,,);//this调用方法
}
public Book(String name,int num) {
this(name,num,);
}
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("老人与海",);
Book book_3 = new Book("陆炳勋",,);
System.out.println(book_0.getInfo());
System.out.println(book_1.getInfo());
System.out.println(book_2.getInfo());
System.out.println(book_3.getInfo());
}
}