Java Programming thought study Note _3

Source: Internet
Author: User

    • Ensure proper cleanup when inheriting

  In the cleanup method, you must also pay attention to the order in which the base class cleanup methods and member object cleanup methods are called, to prevent the occurrence of a child object that relies on another sub-object. In general, all specific cleanup actions for a class are performed first, in the same order as the build order ( Typically this requires that the base class elements remain alive. Then call the base class's cleanup method.

    • Final parameter

  Java allows you to declare parameters as final in a parameter list declaratively. This means that the value of the parameter cannot be changed in the method. (The reference cannot change the object that the parameter points to).

    • Final and Private keywords

All private methods in a class are implicitly specified as final and cannot be overwritten because the private method cannot be taken. You can add a final modifier to the private method, But this does not add extra meaning to the method. Overrides occur only when a method that can transform an object upward and invoke it, if the method is private, is not part of the base class interface, but is hidden from the program code in the class, when declaring a public Protected and package access methods, the method does not produce a case where only the same name appears in the base class. There is no overriding method at this point, just a new method is generated. The following code illustrates this problem:

 Public classDad {Private voidsay () {System.out.println ("I am Dad"); }     Public Static voidMain (string[] args) {Dad d=NewSon (); //The say of a subclass's public access is equivalent to a completely new method defined in a subclass that does not conform to dynamic binding and still uses the Say method of the parent classD.say ();//output I am Dad    }}classSonextendsdad{ Public voidsay () {//The private method of the parent class cannot be accessed in the subclass, and the private wrapper is not accessible within the parent class object inside the subclass object. //Super.say () error. Access to the class cannot be accessed.System.out.println ("I am son.")); }}
    • Initialization and loading of classes

The following code provides an example of initialization:

Import Staticnet.mindview.util.print.*;classInsect {Private intI=9; protected intJ; Insect () {print ("I=" +i+ "j=" +j); J=39; }    Private Static intX1=printinit ("Static insect.x1 initialized."); Static intPrintinit (String s) {print (s); return47; }} Public classBeetleextendsinsect{Private intK=printinit ("BETTLE.K initialized");  PublicBeetle () {print ("K=" +k); Print ("J=" +j); }    Private Static intX2=printinit ("Static bettle.x2 initialized");  Public Static voidMain (string[] args) {print ("Beetle Constructor:"); Beetle b=NewBeetle (); }}

First, when running Java on Beetle, the view accesses Beetle.main, so the loader starts to start and finds the Beetle class's compiled code, and in the process of loading it, the compiler notices that it has a base class, so it continues to load, This can happen regardless of whether you intend to produce a base class object.

Next, the static element of the base class is initialized, followed by the next subclass, and so on. This is important because the static initialization of a subclass may depend on whether the class member is initialized correctly. At this point, the necessary classes are loaded, objects can be created. First, all the base types in the object are set to the default values, the object's reference is set to null-, and the constructor of the base class is called, the base class constructor and the subclass are the same, and the instance variables are initialized in the same order after the base class constructor completes. Finally, The remainder of the constructor is executed.

    • The domain when polymorphic occurs

  Only normal method calls can be polymorphic. If you access a domain directly, the access begins to parse at the compiler:

classSuper { Public intField=0;  Public intGetField () {returnfield; }}classSubextendsSuper { Public intField=1;  Public intGetField () {returnField//equivalent to return This.field.    }} Public classTest3 { Public Static voidMain (string[] args) {Super sup=NewSub (); System.out.println ("Sup.field=" +sup.field+ ", sup.getfield=" +sup.getfield ());//sup.field=0,sup.getfield=1    }}

When a sub object is transformed into a super reference, any domain access operation will have compiler parsing and therefore not polymorphic. The sub actually contains two fields called field, its own and obtained from the super. The default domain generated when referencing a field field in a sub is not a super version of the Field field.

Java Programming thought study Note _3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.