[Javase Study Notes]-7.3 overload of Constructors

Source: Internet
Author: User

[Javase Study Notes]-7.3 overload of Constructors

In this section, we will learn about the overload of constructors.

When it comes to overloading, we have learned in the function chapter. To better understand the overloading of constructors, we must first understand the problem:

When will the constructor be defined?

When describing a thing, the thing already exists and has some content, which is defined in the constructor.

Let's look at the example:

class Person{private String name;private int age;Person(){System.out.println("person run");}public void speak(){System.out.println(name+":"+age);}}
Let's test:

Class ConsDemo {public static void main (String [] args) {Person p = new Person (); // at this time, the constructor p. speak () ;}} is called ();}}

Result:


The print result is null and 0, which means that we have never assigned a value to the member variable.

When we want to create a Person object, the member variable has a value. We can define such a constructor:

Class Person {private String name; private int age; Person () // constructor, which respectively assigns {name = "baby"; age = 1; System. out. println ("person run");} public void speak () {System. out. println (name + ":" + age );}}
Test

class ConsDemo{public static void main(String[] args) {Person p = new Person();p.speak();}}
We can see the result:

If we want a Person object to have a name at birth, we can define it as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> class Person {private String name; private int age; Person () // constructor, assign a fixed value {name = "baby"; age = 1; System. out. println ("person run");} Person (String n) // constructor, with an initial name parameter {name = n;} public void speak () {System. out. println (name + ":" + age );}}

Let's test:

Class ConsDemo {public static void main (String [] args) {Person p = new Person (); p. speak (); Person p1 = new Person ("wangcai"); p1.speak ();}}

Take a look at the results:


Similarly, we can define another constructor to initialize both the name and age parameters.

Class Person {private String name; private int age; Person () // constructor, which respectively assigns fixed values to the member variables name and age {name = "baby "; age = 1; System. out. println ("person run");} Person (String n) // constructor, with an initial name parameter {name = n;} Person (String n, int) {name = n; age = a;} public void speak () {System. out. println (name + ":" + age );}}
Test:

Class ConsDemo {public static void main (String [] args) {Person p = new Person (); p. speak (); Person p1 = new Person ("wangcai"); p1.speak (); Person p2 = new Person ("Xiaoqiang", 10); p2.speak ();}}
Result:

From the above we can see that multiple constructors are defined in a class and their parameters are different. This phenomenon is the overload of the constructor.

Through the overload of constructor, We can initialize different objects through different constructor.

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.