In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the definition and method overloading of Java constructor". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
1. Define the constructor
2. Priority of initialization method
3. Method overload
4. Summary
In methods and data members, we mentioned that objects in Java are initialization when they are created. When initialized, the data members of the object are assigned initial values. We can initialize it explicitly. If we do not assign an initial value to the data member, the data member will adopt the default initial value according to its type. Explicit initialization requires us to determine the initial value when we write the program, which is sometimes inconvenient. We can use the constructor to initialize the object. The constructor can initialize data members and specify specific operations. These operations are performed automatically when the object is created
1. Define the constructor
A constructor is a method. Like the normal method, we define the constructor in the class. The constructor has the following basic characteristics:
The name of the constructor is the same as that of the class
The constructor did not return a value
We define the constructor for the Human class:
Public class Test {public static void main (String [] args) {Human aPerson = new Human; System.out.println (aPerson.getHeight ());}} class Human {/ * constructor * / Human (int h) {this.height = h; System.out.println ("Ihumm born") } / * accessor * / int getHeight () {return this.height;} int height;}
The above program will print
Isimm born
one hundred and sixty
The constructor can receive a list of parameters like a normal method. Here, the constructor Human () takes an integer as a parameter. In the body of the method, we assign the integer parameter to the data member height. The constructor does two things when the object is created:
Provide the initial value this.height = h for the data member
Perform a specific initial operation System.out.println ("Ihumm born")
In this way, we can flexibly set the initial value when calling the constructor without having to be as constrained as the display initialization.
How is the constructor called? When we create classes, we all use the method of new Human (). In fact, we are calling the constructor of the Human class. When we do not define this method, Java provides a blank constructor that can be called when using new. But when we define a constructor, Java invokes the defined constructor when the object is created. When called, we provide a parameter of 160. As you can see from the final run result, the height of the object is indeed initialized to 160.
2. Priority of initialization method
In the method and data members, we can see that if we provide explicit initial values, then the data members will use explicit initial values instead of default initial values. But if we provide explicit initial values and initialize the same data member at the constructor, the final initial value will be determined by the constructor. For example, the following example:
Public class Test {public static void main (String [] args) {Human aPerson = new Human; System.out.println (aPerson.getHeight ());}} class Human {/ * constructor * / Human (int h) {this.height = h;} / * accessor * / int getHeight () {return this.height } int height=170; / / explicit initialization}
The running result is:
one hundred and sixty
The final initialization value of the object is consistent with the value in the build method. Therefore:
Build method > explicit initial value > default initial value
(in fact, the so-called priority is related to the order of execution at initialization, which I will delve into later.)
3. Method overload
More than one constructor can be defined in a class, such as:
Public class Test {public static void main (String [] args) {Human neZha = new Human (150, "shit"); System.out.println (neZha.getHeight ());} class Human {/ * constructor 1 * / Human (int h) {this.height = h; System.out.println ("Ihumm born") } / * constructor 2 * / Human (int h, String s) {this.height = h; System.out.println ("Ne Zha: Ihumm born," + s);} / * accessor * / int getHeight () {return this.height;} int height;}
Running result:
Ne Zha: iTunm born, shit
one hundred and fifty
Two constructors are defined above, both named Human. The two constructors have different parameter lists.
When you create an object using new, Java decides which constructor to build based on the parameters provided. For example, when building neZha, we provide two parameters: the integer 150 and the string "shit", which corresponds to the parameter list of the second build method, so Java invokes the second build method.
In Java, Java determines which method to call based on both the method name and the parameter list, which is called method overloading (method overloading). Build methods can be overloaded, or normal methods can be overloaded, such as the following breath () method:
Public class Test {public static void main (String [] args) {Human aPerson = new Human (); aPerson.breath (10);}} class Human {/ * breath () 1 * / void breath () {System.out.println ("hu...hu...") } / * breath () 2 * / void breath (int rep) {int i; for (I = 0; I
< rep; i++) { System.out.println("lu...lu..."); } } int height;} 运行结果: lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... lu...lu... 可以看到,由于在调用的时候提供了一个参数: 整数10,所以调用的是参数列表与之相符的第二个breath()方法。 4、总结 constructor特征: 与类同名,无返回值 constructor目的: 初始化,初始操作 方法重载: 方法名 + 参数列表 ->Which method is actually called
This is the end of the content of "definition and method overloading of Java's constructor". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.