static应用案例
编写一个程序类,这个类可以实现实例化对象的统计,每一次创建新的实例化对象都可以进行一次统计
class Book{
private String name;
private static int count = 0;
public Book(String name) {
this.name = name;
count++;
System.out.println("第"+count+"本书已经完成创建,名为 "+this.name);
}
}
public class Main {
public static void main(String[] args) {
new Book("三国演义");
new Book("西游记");
new Book("水浒传");
new Book("红楼梦");
}
}
运行结果为:
完美完成了书本的统计
代码编译器:IntelliJ IDEA 2020.02