//static定义可以使类中定义的此类属性让各个对象共享,定义的方法也同理
//static定义的属性和方法不受对象实例化限制。可用类名直接调用
//只能访问static定义的属性和方法,不能访问普通方法
/*
class Book {
public static int num = 0;//统计实例化对象的数目
public Book() {
num++;
System.out.println("产生了" + num + "个新对象");//利用没实例化一个对象就会调用其构造函数
}
}
public class TestDemo {
public static void main(String args[]) {
new Book();
new Book();
new Book();
new Book();
new Book();
}
}
*/
//无论对象又参无参都要为title设置内容
class Book {
private static int num = 0;
private String title;
public Book() {
this("NUM--" + ++num);
}
public Book(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
}
public class TestDemo {
public static void main(String args[]) {
System.out.println(new Book("aaa").getTitle());
System.out.println(new Book().getTitle());
System.out.println(new Book().getTitle());
}
}