//: Frog.java
// Testing finalize with inheritance
class DoBaseFinalization {
public static boolean flag = false;
}
class Characteristic {
String s;
Characteristic(String c) {
s = c;
out.println(
"Creating Characteristic " + s);
}
protected void finalize() {
out.println(
"finalizing Characteristic " + s);
}
}
class LivingCreature {
Characteristic p =
new Characteristic("is alive");
LivingCreature() {
out.println("LivingCreature()");
}
protected void finalize() {
out.println(
"LivingCreature finalize");
// Call base-class version LAST!
if(DoBaseFinalization.flag)
try {
super.finalize();
} catch(Throwable t) {}
}
}
class Animal extends LivingCreature {
Characteristic p =
new Characteristic("has heart");
Animal() {
out.println("Animal()");
}
protected void finalize() {
out.println("Animal finalize");
if(DoBaseFinalization.flag)
try {
super.finalize();
} catch(Throwable t) {}
}
}
class Amphibian extends Animal {
Characteristic p =
new Characteristic("can live in water");
Amphibian() {
out.println("Amphibian()");
}
protected void finalize() {
out.println("Amphibian finalize");
if(DoBaseFinalization.flag)
try {
super.finalize();
} catch(Throwable t) {}
}
}
public class Frog extends Amphibian {
Frog() {
out.println("Frog()");
}
protected void finalize() {
out.println("Frog finalize");
if(DoBaseFinalization.flag)
try {
super.finalize();
} catch(Throwable t) {}
}
public static void main(String[] args) {
if(args.length != 0 &&
args[0].equals("finalize"))
DoBaseFinalization.flag = true;
else
out.println("not finalizing bases");
new Frog(); // Instantly becomes garbage
out.println("bye!");
// Must do this to guarantee that all
// finalizers will be called:
System.runFinalizersOnExit(true);
}
} ///:~
P.201的这段代码中,System.runFinalizersOnExit(true)已废弃,有没有替代函数可用,网上找了半天无果,哪位大神愿指点一二。谢谢!
标签:13,Java,finalize,Characteristic,void,第四版,println,class,out From: https://blog.51cto.com/u_16183536/7090588