Java constructor understanding

Introducing constructor

In Java, by providing a configuration, a designer can ensure that each class of objects are initialized. When you create an object, if it has the class constructor, Java will be the user has the ability to automatically call the appropriate constructor before the operation object. Thus ensuring initialized.

Constructor named

  1. The name may have to obtain any conflict with the name of a member of the class
  2. Call the constructor is the responsibility of the compiler too, it is necessary to let the compiler know which method to call

Java class constructor takes the same name

class Example{
    public String var1;
    public String var2;
    // 默认构造器(无参构造器)
    public Example(){}
}

Form constructor

We do not accept any argument constructor called the default constructor. However, like other methods, Java constructor with a parameter can, in order to specify how to create the object.

class Example{
    public String var1;
    public String var2;
    // 默认构造器(无参构造器)
    public Example(){}
    
    //自定义构造器(带有形参)
    public Example(String var1,String vart2){
        this.var1 = var1;
        this.var2 = var2;
    }
    
    public static void main(String[] args){
        Example example = new Example("参数1","参数2");
    }
}

In the above code, there are two constructors (custom default constructor and builder), so we can use new Example()the way to create an object with no parameters Example also be used new Example(var1,var2)the way to create an object.

Configuration helps reduce errors, and make the code easier to read. Conceptually, "initialization" and "Create" are independent of each other, but in Java, "initialization" and "Create" are tied together, the two can not be separated.

The constructor has no return value, does not return anything (new expression does return a reference to the new object, but the structure itself does not have any return value)

this keyword

this key can only be used in internal methods, is a reference to "an object which call method" of. Here are six use this keyword:

  1. this may be used to refer to the current class instance variable.
  2. this method can be used to invoke the current class (implicit)
  3. this () can be used to call the constructor of the current class
  4. this can be passed as a parameter in the method call.
  5. this can be passed as parameters in the constructor call.
  6. this can be used to return from the current instance of the class method.

For details refer to link 3

super keyword

The constructor can not be inherited, for subclasses to call the parent class constructor, then you must use the keyword to super.

package chapter7;

class Example {
    public String name;

    //无参构造器
    public Example(){
        System.out.println("Example()");
    }

    //有参构造器
    public Example(String name){
        this.name = name;
        System.out.println(name);
    }
}

public class People extends Example{
    public People(){
        super();
    }
    public People(String name){
        super(name);
    }
    public static void main(String[] args){
        People people1 = new People();
        People people2 = new People("yoloyanng");
    }
}
/*
* Output:
* Example()
* yoloyanng
*/

People included in the class constructor two super keyword, their role is instantiated parent class constructor added to the People. And the super keyword can only be placed on the first line, and if not coupled with the super keyword, the compiler will automatically add.

More usage:

  1. It can be used to directly reference the parent super class instance variables.
  2. super可用于直接调用父类方法。
  3. super() 可用于直接调用父类构造函数。

详细内容参考链接4

参考

《Java编程思想》(第四版)

链接1:https://www.cnblogs.com/grl214/p/5895854.html

链接2:https://blog.csdn.net/f381226200/article/details/88368446

链接3:https://www.javatpoint.com/this-keyword

链接4:https://www.javatpoint.com/super-keyword

Guess you like

Origin www.cnblogs.com/CH42e/p/12354493.html