62,621
社区成员
发帖
与我相关
我的任务
分享public class TerminationCondition {
public static void main(String[] args) {
Book novel = new Book(true);
novel.checkIn();
new Book(true);
System.gc();// 在没有创建对象的函数中,在main()方法中由于成程序员的错误,有一本书没有嵌入,要是没有finalize()来验证终结条件,将很难发现这种缺陷。
Book novel2 = new Book(true);
System.gc();// 这里不需要使用system.gc()来终结条件,因为在这里的这个对象中已经分配了内存,所以
// 系统回收机制会自动的释放这个内存。
}
//没有释放的运行结果:The type Book is already defined(类型Book已经被定义了);
}
class Book {
boolean checkedout = false;
Book(boolean checkout) {
checkedout = checkout;
}
void checkIn() {
checkedout = false;
}
protected void finalize() {
if (checkedout) {
System.out.println("error:checked out");
}
}
}public class TerminationCondition {
public static void main(String[] args) {
Book1 novel = new Book1(true);
novel.checkIn();
new Book1(true);
System.gc();// 在没有创建对象的函数中,在main()方法中由于成程序员的错误,有一本书没有嵌入,要是没有finalize()来验证终结条件,将很难发现这种缺陷。
Book1 novel2 = new Book1(true);
System.gc();// 这里不需要使用system.gc()来终结条件,因为在这里的这个对象中已经分配了内存,所以
// 系统回收机制会自动的释放这个内存。
}
//没有释放的运行结果:The type Book is already defined(类型Book已经被定义了);
}
class Book1 {
boolean checkedout = false;
Book1(boolean checkout) {
checkedout = checkout;
}
void checkIn() {
checkedout = false;
}
protected void finalize() {
if (checkedout) {
System.out.println("error:checked out");
}
}
}
运行结果:error:checked out