Java constructor, super, this, final, static


  A, java class is generated automatically create a constructor with no arguments
    custom constructor:
      public class name (parameter list) {

       }

  Note: 1, no return value Constructor

     2, the constructor name must be consistent and class names

     3, access modifier constructor must be public or can not be instantiated

     4, a custom constructor generated java constructor Alternatively, it is recommended not declare a constructor parameter, so as to be inherited.

 

  Two, this keyword

    1, this representative of the current through the object class is instantiated.

    2, to obtain the properties and methods of this current class by

    3, and this by distinguishing member variables and local variables

    4, by this (parameter), the current class constructor call Note: this by calling the constructor of the current class is the first line of the line into the current method for an

 

  Three, super keyword;

    1, super. Property call parent class attributes and methods

    2, super (parameters) to call the parent class constructor

    Note: 1, in case of both sub-class constructor this (argument), there are super (parameters) when, this keyword is still the first line, super (parameters) into the current method of this () constructor function

          2, when the inheritance relationship, the first implementation of the parent class constructor, and then execute the constructor subclass instance subclasses, pass parameters should also comply with the constructor of the parent class and subclass

  

 

  Four, final keyword

    1, modified final category is the final class can not be inherited

    2, the final modification methods can not be overridden, however, the parent class useless as a final method which Subclasses override modifications can be added in the final keyword subclass 

    3, the final properties of the modified unavailable called constant changes Note: The modified final attribute should be assigned before the class is instantiated

 

 

  Five, static keyword

    1, static class can not be modified

    2, static properties and methods can be modified obtained by the class name. Attribute / Method

    3, static modification of properties and methods to enter  data and methods shared area of the static area  all the static properties and methods are modified priority object exists.

    4, not through this / super access

    5, static members can only be accessed through the static member

    6, when the parent class is not a static method subclass overrides this method is not static plus 

    7,  polymorphic method calls, the compiler = look left, there is the parent class, compiled successfully, the father did not, fail to compile

      Run, static method, run the parent class's static methods,

      Overriding methods run, non-static method, run subclass

      Member variables, compile and run all the parent class

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/yanghaoyu0624/p/11566606.html