How to construct the Java tutorial overloaded

Source: Internet
Author: User

Learn Programming Bar programming It tutorial Java Construction method overload tutorial released, Welcome to visit through Xuebiancheng8.com.

In the previous analysis of what is the construction method, the constructor is to instantiate the object when the code is the construction method to complete the initialization of the object, then you think is not in a class can only have a construction method, the answer is no, there can be more than one constructor in a class, These construction methods form the overloads of the method, and let's look at what the constructor method overloads are. Let's look at an example:

public class person{

private int age;

private String name;

Public person () {

SYSTEM.OUT.PRINTLN ("Non-parametric construction method");

}

public person (int age) {

This.age = age;

}

public person (int age,string name) {

This.age= age;

THIS.name = name;

}

}

Okay, so we've written three constructs, one for each parameter, and two for the argument.

So when does the three construction methods actually execute?

Person p = new person ();

Call the parameterless construction method above

Person p = new person (19);

The method of constructing a parameter is called above

Person p = new person ("Zhangsan");

The specific invocation of the constructor method is based on the parameters passed in and the constructor method matches, then the method is executed.

Please visit xuebiancheng8.com for details. The URL is:

Http://xuebiancheng8.com/play/goodgoodstudy_95_daydayup.html


How to construct the Java tutorial overloaded

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.