[Java Basics] constructor call constructor and java Basic Call Constructor

Source: Internet
Author: User

[Java Basics] constructor call constructor and java Basic Call Constructor

Start with a program:

1 class dog 2 {3 private String name; 4 private String color; 5 private int age; 6 7 dog (String name) 8 // only assign 9 {10 this. name = name; 11} 12 dog (String name, String color) 13 // assign a value of 14 {15 this. name = name; // but there has been a similar code 16 this. color = color; 17} 18 dog (String name, String color, int age) 19 // assign 20 {21 this to name, color, and age only. name = name; 22 this. color = color; // similar code 23 has been found before. age = age; 24} 25/* the above Code is defective. The code is repeated. 26. If you modify the initially defined parameter (such as name, 27. A large number of parameters in the method need to be modified (3 points), causing inconvenience */28} 29 30 class ConstructerDemo 31 {32 public static void main (String [] args) 33 {34 35} 36}View Code

In this case, we can use the constructor to call the constructor (in the form of this (real parameter) to solve the above problem. The improvement is as follows:

1 class dog 2 {3 private String name; 4 private String color; 5 private int age; 6 7 dog (String name) 8 // only assign 9 {10 this. name = name; 11} 12 dog (String name, String color) 13 // assign a value of 14 {15 // this. name = name; // but there have been code 16 this (name); // improved 17 this. color = color; 18} 19 dog (String name, String color, int age) 20 // assign a value of 21 {22 // this to name, color, and age only. name = name; 23 // this. color = color; // code similar to 24 this (name, color) before; // the last 25 this. age = age; 26} 27/* the above Code is defective. The code is repeated. 28. If you modify the initial defined parameter (such as name, 29. A large number of parameters in the method need to be modified (3), causing inconvenience */30} 31 32 class ConstructerDemo 33 {34 public static void main (String [] args) 35 {36 37} 38}View Code

Note that the call to this constructor must beConstructorOfFirst Statement.

That is, two points must be noted. First, the this constructor must be placed in the constructor. Second, it must be the first statement in the constructor.


In Java, how does one call a constructor with parameters in the constructor without parameters? I hope there is a better example,

First of all, I was wondering if the landlord had gotten a horn?
Generally, the call parameters with many parameters are few. Most of the calls with parameters without parameters.
Of course, you can call parameters without parameters.
The constructor calls each other. At least ensure that one of them does not use this call.

You can call a request with no parameters. How do you transmit parameters with parameters. Of course, you can also change the angle.
I wrote it by hand. Not through IDE. Provide the following ideas. I have two ways to write the same function.
Public class Person {
Private String name;
Private int age;
// No Parameter
Public Person (){
This. name = "James ";
This. age = 20;
}
// Parameters
Public Person (String name ){
This. name = name;
}
// Multiple parameters
Public Person (String name, int age ){
This (name); // call the constructor of a parameter.
This. age = age;
}
}
This is a common method. Write first. Write directly using the example above
Public Person (){
This ("James", 20); // call the construction method with parameters. Set the default value. Same as the first method
}
Public Person (String name, int age ){
This. name = name;
This. age = age;
}

It is easy to program. Average. Parameter extension and constructor call are both regular.
If the parameter is set, no parameter is set. If the parameter is set to multiple parameters, no parameter is set .. It is best to keep the original order when extending parameters.

Hope to help the landlord. Because it has not been compiled. It is hand-written. If there is any small problem, change it by yourself. The idea is as follows.

In Java, how does one construct a method to call a common method?

1. It is completely usable and is often used in swing programs. In this case, the parent class constructor is called for the first time, but the subclass overwrites the print method and outputs j. At this time, j is not initialized and 0. Then the subclass constructor calls print, at this time, j is initialized to 30, and output to 30. Finally, the changed j is output to 40 in the main method. Instant sequence 03040
To verify that the above execution process is correct, you can debug it or change j to static, so that it will be initialized before calling the parent class, so the sequence is 303040
2. The static method belongs to the class, so the parent class constructor calls the parent class, and the following occurs:
Executed
10
3040

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.