复制代码
- //:Frog.java
- //Testingfinalizewithinheritance
- classDoBaseFinalization{
- publicstaticbooleanflag=false;
- }
- classCharacteristic{
- Strings;
- Characteristic(Stringc){
- s=c;
- System.out.println(
- "CreatingCharacteristic"+s);
- }
- protectedvoidfinalize(){
- System.out.println(
- "finalizingCharacteristic"+s);
- }
- }
- classLivingCreature{
- Characteristicp=
- newCharacteristic("isalive");
- LivingCreature(){
- System.out.println("LivingCreature()");
- }
- protectedvoidfinalize(){
- System.out.println(
- "LivingCreaturefinalize");
- //Callbase-classversionLAST!
- if(DoBaseFinalization.flag)
- try{
- super.finalize();
- }catch(Throwablet){}
- }
- }
- classAnimalextendsLivingCreature{
- Characteristicp=
- newCharacteristic("hasheart");
- Animal(){
- System.out.println("Animal()");
- }
- protectedvoidfinalize(){
- System.out.println("Animalfinalize");
- if(DoBaseFinalization.flag)
- try{
- super.finalize();
- }catch(Throwablet){}
- }
- }
- classAmphibianextendsAnimal{
- Characteristicp=
- newCharacteristic("canliveinwater");
- Amphibian(){
- System.out.println("Amphibian()");
- }
- protectedvoidfinalize(){
- System.out.println("Amphibianfinalize");
- if(DoBaseFinalization.flag)
- try{
- super.finalize();
- }catch(Throwablet){}
- }
- }
- publicclassFrogextendsAmphibian{
- Frog(){
- System.out.println("Frog()");
- }
- protectedvoidfinalize(){
- System.out.println("Frogfinalize");
- if(DoBaseFinalization.flag)
- try{
- super.finalize();
- }catch(Throwablet){}
- }
- publicstaticvoidmain(String[]args){
- if(args.length!=0&&
- args[0].equals("finalize"))
- DoBaseFinalization.flag=true;
- else
- System.out.println("notfinalizingbases");
- newFrog();//Instantlybecomesgarbage
- System.out.println("bye!");
- //Mustdothistoguaranteethatall
- //finalizerswillbecalled:
- System.runFinalizersOnExit(true);
- }
- }///:~
复制代码
- notfinalizingbases
- CreatingCharacteristicisalive
- LivingCreature()
- CreatingCharacteristichasheart
- Animal()
- CreatingCharacteristiccanliveinwater
- Amphibian()
- Frog()
- bye!
- Frogfinalize
- finalizingCharacteristicisalive
- finalizingCharacteristichasheart
- finalizingCharacteristiccanliveinwater
复制代码
- CreatingCharacteristicisalive
- LivingCreature()
- CreatingCharacteristichasheart
- Animal()
- CreatingCharacteristiccanliveinwater
- Amphibian()
- Frog()
- bye!
- Frogfinalize
- Amphibianfinalize
- Animalfinalize
- LivingCreaturefinalize
- finalizingCharacteristicisalive
- finalizingCharacteristichasheart
- finalizingCharacteristiccanliveinwater
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |