Use and significance of Java constructor and constructors

 I. Definition statement constructor

The name of the constructor must match the name of the class where no return value, but can not be declared void, access rights can be any, but the use of public methods rights in general, constructor parameters can define as needed, different parameters constructor overloads configuration;

  Example:

  class Fu

  {

    public Fu () {} // public constructor with no arguments

    public Fu (int i) {} // Constructors public parameters of type of int

    ......

  }

 

  public class Demo extends Fu

  {

    public Demo () {} // public constructor with no arguments

    public Demo (int i) {} // Constructors public parameters of type of int

    public Demo (int i, double d) {} ​​// Constructors public parameters of type int and double the

    ...

  }

 II. Method of construction using

Java is used in the constructor has two places, a new new keyword is followed, a class name with parentheses (), plus a small actual argument in parentheses, with the additional one or keyword super this after adding a small parenthesis (), the parentheses added according to actual arguments, the following Examples.

 Example 1:

 Demo demo = new Demo (); // this is called a non-argument constructor must be declared in the method, preferably the method stated in the main

 The above argument constructor arguments actually added, according to the parameters configured JVM process loaded different configurations of different methods;

Example 2:

 public Demo(){

  this (2); // this class constructor parameter called here type int

 }

Example 3:

 public Demo(){

  super (1); // call the constructor parameter of type int parent class

 }

Note: 2 and 3 in this embodiment or super constructor call can only occur in the constructor, and must appear in the first row, the first row of a constructor or super this can only call the constructor, both when the constructor can not be called simultaneously appear, and pay attention to this or super constructor call, to stay constructor exports, construction method means that there is no last call and then call the other constructor!

 Role of the constructor

 1. In order to initialize member properties, rather than the object is initialized, initialize the object is achieved by the new keyword

 2. new call the constructor initializes the object, compile time checks the signature constructor according to the parameter, referred to as static build and compile polymorphism (signature parameters: type, number of parameters, and the parameter order parameter)

3. Create a subclass object will call the parent class constructor but does not create the parent object, just call the parent class constructor to initialize the parent class member properties; I always want to be confused constructors and methods, and later found that the process, in fact, , is the need for performing java code, and constructors, the constructor is an instance of a class ,,,! !

why?

Instance of a class, we need to create a class object, and then access its properties, because the instance is to be used to call, but the call, we have to consider a question, is the object, it is ultimately stored in the memory inside , and storage, we need to have a memory of him to open up another new memory space, then, java is how to give this class we need to open up the memory space it? This involves the memory mechanism java, that is, we have to give to make this class a constructor, and the name of the constructor must be the name of the class is the same, so our java compiler to recognize this class , and then to open up the class in memory, memory space, which is what we said, we hand, man to give him a "initialize", the following examples:

  class Rock {

    Rock() {

      System.out.print("Rock");

    }

  }

  In this way, when we call the class of the Rock, our java compiler will advance to him, "Auto" initialize, open up the memory space

  So now the problem again, for example, our Rock () method requires a single parameter, parameter, but the entire code, it is necessary not only Rock with the parameter (); parameter with no further need of Rock (), when we constructor for class configuration, requires similar functions, but different parameter while packaged in the methods of the class, so that when we call a method of directly override the constructor in this method, it can be said that this form of construction, we meet a similar function, parameter different methods, when called, overloaded, and to meet the compiler automatically initialized, people do not need to demand a manual initialization.

  But there is a problem, had two methods are functionally similar, and a tree sapling, you have to give them different names, more awkward, but fortunately with the constructor, is capable of similar functions method from the same name, different parameters, and can be reloaded when they were called, how powerful constructor ah.

Guess you like

Origin www.linuxidc.com/Linux/2020-03/162643.htm