I. Inheritance and cleanup
If a class needs to clean up its own resources, it must create a garbage collection method for it, and if this class has an exported subclass, you must overwrite the reclaimed method in the exported class, and when overwriting the garbage collection method of the inherited class, you need to be aware that the order of destruction should be the reverse of the initialization. For fields, it means the opposite of the order of declarations. The export class should be cleaned first and then the base class. This is because the cleanup of the exported classes might call some of the methods of the base class, so you need to make the base class artifacts still work without destroying them prematurely.
This can become complicated if you need to destroy variables that are shared by multiple member objects. In this case, you can use reference counting to access the number of code for the shared object, with the following code:
Import Staticnet.mindview.util.print.*;classShared {Private intrefcount=0;//identifies the number of references to the variable Private Static LongCounter=0; Private Final Longid=counter++;//Identify shared variables PublicShared () {print ("Creating" + This); } Public voidAddRef () {RefCount++; } protected voidDispose () {if(--refcount==0) {print ("Disposing" + This); } } PublicString toString () {return"Shared" +ID; } }classComposing {PrivateShared shared; Private Static LongCounter=0; Private Final Longid=counter++; Publiccomposing (shared shared) {print ("Creating" + This); This. shared=GKFX; This. Shared.addref (); } protected voidDispose () {print ("Disposing" + This); Shared.dispose (); } PublicString toString () {return"Composing" +ID; }} Public classreferencecounting { Public Static voidMain (string[] args) {shared shared=Newgkfx (); Composing[] Composing={NewComposing (shared),Newcomposing (shared),NewComposing (shared),Newcomposing (shared)}; for(composing c:composing) {c.dispose (); } }}
Behavior of polymorphic methods within the constructor
If you call a dynamically bound method inside the constructor, you need to use that method's overridden definition, and the dynamically bound method calls the method of the exported class, at which point the exported class member variable is not initialized and may produce an error.
Public classTest { Public Static voidMain (string[] args) {NewNewSon (). print (); }}Abstract classFather {String fs= "dad!"; PublicFather () {print (); } Public Abstract voidprint ();}classNewSonextendsFather {intI=4; String SS= "son!"; Public voidprint () {System.out.println ("Son." +i);//System.out.println (Ss.touppercase ()); Error }}
Small Details : If a class implements an interface and inherits another class, if the inherited class has a method that is signed with the same method in the interface, the subclass can not provide an implementation for the method. The method inherited by the parent class is equivalent to implementing an interface in a subclass.
Interface Eat { publicvoid Eat ();} class Dog { publicvoid eat () { }}publicclass extends Implements Eat {}
Java Programming thought study note _4