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/01 Report--
In this article Xiaobian detailed introduction of "Java constructor how to use", the content is detailed, the steps are clear, the details are handled properly, I hope this "Java constructor how to use" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
Constructor
Before we learn, we must first understand what is a constructor? What can a constructor do?
Constructor: the constructor we mentioned earlier, also known as the constructor, is a special function in Java.
Role: constructors are generally used to initialize member properties and member methods. When we use the new keyword beauty to create a new object, the constructor is called once.
Characteristics of constructor
Function name must be the same as class name.
There is no need to define the return value type.
Each object has at least one constructor, and even if no constructor is created, a no-parameter constructor is added by default.
If there is a custom constructor, the system will not add it.
Because the constructor name can only be the same as the class name, which means that if you want to customize multiple constructors, then there must be the result of duplicate names. Here, we will first introduce another very important knowledge point in Java-overloading.
Heavy load
Definition: in the same class, a method can have the same method name, but given different parameters, so this is called method overloading (Method Overloading).
Features: it has nothing to do with the return value type, but only with the parameter list. (number of parameters, parameter type, parameter order).
So after the method is overloaded, will it affect the previously written method function?
The answer is no! Method overloading does not override the previous method function.
Same method name, different method function.
Example usage of constructor no-parameter constructor
Create a People object and define a no-parameter constructor (to see more intuitively whether the constructor is called or not, add a print to it. )
Public class People {String name; Integer age; String gender; public People () {System.out.println ("this no-parameter constructor has been called.") ;}}
Create a new object with the new keyword
Public class Demo {public static void main (String [] args) {People p1 = new People ();}} has a parametric constructor
The parameter constructor has more parameter types and parameter values than the non-parameter constructor.
Writing method
Public class name (parameter type 1 parameter value 1, parameter type 2 parameter value 2) {}
Customize a parametric constructor
Public People (String name, Integer age, String gender) {this.name = name; this.age = age; this.gender = gender;}
Create an object named p2 to call a custom parametric constructor
People p2 = new People ("Jack", 15, "male"); System.out.println (p2.name+ "is a" + p2.age+ "age" + p2.gender + "student.")
Seeing here, I have a general understanding of the constructor. Next, let's talk about the difference between constructors and ordinary functions.
The difference between constructor and ordinary function
The constructor looks very similar to the normal function, just like twins. But even for twins, there will be some differences before.
The function name of an ordinary function can be customized according to your personal preference; the function name of the constructor can only be the same as the class name.
Ordinary functions can be decorated with void or have return values, while constructors can neither be decorated with void nor return values.
In general, it is recommended that ordinary functions use small hump naming, that is, the first letter is lowercase; constructors and class names should be capitalized.
Ordinary functions can be called directly through the function name (); constructors are called when the new keyword creates an object.
After reading this, the article "how to use Java Constructor" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.
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.