[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.