In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Java construction method and method overload what the characteristics of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java construction method and method reload what the characteristics of the article will have a harvest, let's take a look.
The structure of the class includes:
1. Member variable
two。 Member method
3. Construction method
4. Code block
5. Inner class
The function of the first construction method
It mainly has the following three functions:
(1) initialize the assignment to the created object in the constructor
(2) when creating an object, at least one constructor needs to be called.
(3) every class has a constructor.
An example deepens the understanding of the above three.
Public class Car {String name; String color; float price;}
Name, color, and price are all member variables of the class. Let's create a class TextCar to test. For example, now remember:
Car bm = new Car ()
Car () is the constructor in this code. So it also shows that (2) when creating an object, at least one constructor needs to be called.
Public class TextCar {public static void main (String [] args) {Car bm = new Car (); / / create (new) an object System.out.println (bm.name) for the Car class;}}
Name is not assigned in the main method, but the result is as follows:
It is not difficult to find that the program can normally run and the value of name is null. In fact, the constructor assigns an initial value to the member variable, that is, (1) initializes the assignment to the created object. The construction method assigns initial values such as: int type initial value: 0, double type initial value: 0. 0, String type initial value null, and so on.
There are only member variables, no member methods, and no constructors in the public class Car {} class. In fact, because we don't have a book-writing constructor shown, there is an implicit default constructor in the class:
Public class Car {String name; String color; float price; public Car () {/ / default construction method, which can also be displayed and written out this.name = null; this.color = null; this.price = 0;}}
Public Car () {} has an implicit no-parameter constructor, but what we show in the above code is written, which explains (3) every class has a constructor.
The knowledge points of the construction method are sorted out here. You are welcome to add in the comments area.
The characteristics of the second construction method
The characteristics of the construction method are as follows:
(1) the name and class name are the same (this needs to be kept in mind)
(2) No return value
(3) there is no need to modify it with void.
There is a default no-parameter constructor Car () {}} in public class Car {/ /
The default no-parameter constructor in the Car class is also called Car ().
We can also define a parametric constructor. If we define a parameterized constructor, the default constructor will be overwritten. For example:
Public class Car {String name; String color; float price; public Car (String name,String color,float price) {this.name = name; this.color = color; this.price = price;}}
At this point, the default no-parameter construction method is overwritten.
If you are just in contact with java, it is important for this. Do not understand, take a look at the simple explanation of this paragraph: this represents the corresponding object, you must find the corresponding object in the main method, such as the code in the previous paragraph, you can find such an object: Car bm = new Car ("BMW", "black", 5000000); (parameter as long as the type is determined, the reader is free to write whatever you want), this refers to this object, this. The following name represents the member variable name of the Car class, and the name to the right of the equal sign represents the parameter name of the constructor. The rest of the variables are the same. Finally, if you find it difficult to understand, you can change the parameter to another name, such as a, this on the left. It can be removed. But the use of this must be able to.
To a complete and simple code, the reader taps it, and then tells it to himself, and it will be fully understood.
Public class Car {String name; String color; float price; public Car (String name,String color,float price) {this.name = name; this.color = color; this.price = price;} public run () {/ / member method System.out.println ("car driving") }} public class TextCar {public static void main (String [] args) {Car bm = new Car ("BMW", "Red", 500000); / / create (new) an object System.out.println (bm.name+ "" + bm.color+ "" + 500000) for the Car class; bm.run ();}} method overload
1. What are the characteristics of overloading
(1) the phenomenon that multiple methods have the same name in a class
(2) both constructors and member methods can be overloaded
(3) whether it is the same method by the order of the number, type and type of parameters.
Method overloading involves a problem of overriding. If you redefine the construction method of passing parameters, you need to write out the default no-parameter construction method, because the default has been overridden, and an error will be reported when calling Car ()! For example:
Public Car (String name,String color,float price) {this.name = name; this.color = color; this.price = price; public Car (String name,String color,float price) {this.name = name; this.color = color; this.price = price;} public run (String name,String color) {/ / member method System.out.println (this.color+this.name+ "car driving") } public run (String color,String name) {System.out.println (this.color+this.name+ "driving");} public int run (String name) {System.out.println (this.name+ "driving"); return 0;}} public class TextCar {public static void main (String [] args) {Car dz = new Car () / create an object System.out.println (dz.name); Car bm = new Car ("BMW", "red", 500000); / / create an object System.out.println (bm.name+ "+ bm.color+"+ 500000); bm.run (" red "," BMW "); bm.run (" BMW ");}}
Because the non-parameter construction method Car () has been covered by Car (String name,String color,float price) with parameters, the no-parameter construction method can not be found in new Car (). We need to show that we have to define Car ().
Method overloading, there is a constructor with the same name of Car in the Car class, and a constructor with the same name of run (). The method that uses the same name in the class is the overload of the method. If the name and parameter type, quantity and order of the two methods are the same, it is not allowed. The method of the same name needs to distinguish the parameter type, quantity and order of the method.
This is the end of the article on "what are the characteristics of Java construction methods and method overloading". Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the characteristics of Java construction methods and method overloading". If you want to learn more knowledge, you are welcome to 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.