Java-Preliminary Understanding-seventh-constructor overloading

Source: Internet
Author: User

One.

The topic is a constructor overload, you can think of the previous overload of the general function, the concept is always interconnected.

What is a certain thing, what is the feature after the introduction, the following is how to use (Initialize), when to use?

Two.

When do you define a constructor?

Some of the things that are present when a thing is described are defined in the constructor.

The general approach is to say that these things have this function, and we define it in a general way. And in real life, the object of the creation of all things, are defined in the constructor. We have not commanded it as soon as we have it, we define it in the constructor, the command is the other function of the call.

Explained by a source: (now, what we're going to talk about is what happens in the constructor.) )

Above is the description class and the creation class, if compiled, run, the result is as follows. Two output statements, one output from the constructor, and one from the general function speak ().

After the program runs, the result of the output is null, and the member variable is not assigned, and is the default value. We hope that the object will have some content as soon as it appears. No matter what, as soon as the appearance, there are some content.

Then, the source program is modified, in the constructor, the member variable is assigned ( new object, constructor initialization, the member variable is assigned to the member variable and directly in the class to assign value what is the difference ?) As soon as a person is born, there is a definite name and age. Run the program again with the following results:

The JVM enters from the main function, executes two steps, executes the constructor when the new object is created, and then executes the general function. In the constructor, the member variables defined in the class are assigned, the person run is printed, and the member variable is output when the general function is called (at this point, the member variable has been assigned a value). We think that when a baby is born, it should have a default name and a default age. It also means creating more babies, which is a feature of this. Because these babies are initialized here from the constructor function.

Expand, and change the initialization of the member variables in a different place. The following is the placement of the initialization in a general function, which leads to the problem that the member variables are initialized only when the general function is called, which is intended to be contrary to the original intent. The beginning is the thing that requires the object to be born. If you do not call the Speak () function, you do not have a name and age when you create a new object.

In another case, a child is born with a name, and the child is set up with a value. This means that it is not the initialization of the above constructor.

This produces two constructors, meaning that different constructors can initialize different objects.

For the above two kinds of constructors, after different initialization of variables, the result is as follows.

There is always a problem bothering me, why not assign a value directly to the member variable definition? I guess it might be that when you create different objects, you can't have all the same values for all objects. More advanced, it is a matter of whether the member variables in the description class are private and can be assigned directly.

Then define more constructors, which are all assigned to the object . Can the constructor understand this? As I thought before, assigning values to member variables is different. )

The results of the operation are as follows

This takes the constructor to initialize the object in a way that, when you create a new object, you enter parameters in parentheses in the object.

In a class, you can have multiple constructors that can initialize n multiple objects. Multiple constructors appear in a class, and there can be only one form of their existence, which is called overloading.

Overloading what characteristics, the method name is the same, the parameter list is different (number of parameters, parameter type).

These two constructors are not the same, Java rigor, which is overloaded. The order of the parameters is also important, such as two parameters int A, Boolean B. If a data input is true, that is not appropriate, which indicates that the parameter order is also important. When learning the parameter list in a functional function, there is formal parameter 1, form parameter 2, which also indicates that the order of parameters is very important.

In a class, when more than one constructor appears, they must be represented in an overloaded form. It can give different objects, different initialization.

Java-Preliminary Understanding-seventh-constructor overloading

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.