class Book {
private String title;
private static int count = 0;
public Book(){ // 无参构造
this("NOTITLE - " + count++);// 避免了没有title的情况,调用下面的构造方法,把“NOTITLE - count”当作title
}
public Book(String title){
this.title = title;
}
public String getTitle(){
return this.title;
}
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println(new Book("Java").getTitle());
System.out.println(new Book("JSP").getTitle());;
System.out.println(new Book("Spring").getTitle());
System.out.println(new Book().getTitle());
System.out.println(new Book().getTitle());
}
}
标签:title,System,案例,Book,static,getTitle,5.15,new,out
From: https://www.cnblogs.com/pansidong/p/17452199.html