Home  >  Article  >  Java  >  What is the use of java's parameterized constructor?

What is the use of java's parameterized constructor?

(*-*)浩
(*-*)浩Original
2019-05-05 14:56:356139browse

Parameterized construction in Java is a type of construction method, and the function of the construction method is to call the constructor to complete the initialization of the object when new an object. In order to initialize member properties, but not to initialize the object, initialize the object. This is achieved through the new keyword.

What is the use of java's parameterized constructor?

#If you want to understand the use of parameterized construction, you must first know the use of constructor methods, because parameterized construction is a type of construction method.

The function of the constructor (constructor):

When new an object, the constructor is called to complete the initialization of the object. In order to initialize the member properties, rather than initializing the object , the initialized object is implemented through the new keyword.

Initialize the object by calling the constructor method with new, and check the constructor according to the parameter signature during compilation, which is called static binding and compilation polymorphism (parameter signature: type of parameter, number of parameters and parameter order)

Creating a subclass object will call the parent class constructor but will not create the parent class object. It will just call the parent class constructor to initialize the parent class member properties;

The parameterized constructor is used to perform the function of the constructor, for example:

 class Person{
    int age;
    //有参构造方法
    public Person (int a){
        age=a;
    }
    public void speak(){
        System.out.println("我今年"+age+"岁");
    }
}

public class Test02{
    public static void main (String [] args){
        Person p=new Person (20);//实例化同时赋值
        p.speak();
    }
}

The above is the detailed content of What is the use of java's parameterized constructor?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is servlet filterNext article:What is servlet filter