Introduction to the author
Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!
introduction
Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!
Navigation
✪ introduction to Java white 200 case series directory index
◄ previous article 85. Why use up transformation instead of directly creating subclass objects
► next article to be updated
summary
Java allows multiple methods with the same name to be defined in the same class as long as their formal parameter lists are different. If the same class contains two or more methods with the same method name but different formal parameter lists, this situation is called method overload.
Example 1
Overloads with different number of parameters.
package demo.demo86; public class OverLoading { public void print(){ System.out.println("Method without parameters print"); } public void print(String a){ System.out.println("Method of one parameter print"); } public void print(String a,String b){ System.out.println("2 Method with two parameters print"); } public void print(String...strs){ System.out.println("n Method with two parameters print"); } public static void main(String[] args) { OverLoading ol = new OverLoading(); ol.print(); ol.print("a"); ol.print("a","b"); ol.print("a","b","c"); } }
function:
Method print without parameters
One parameter method print
Method print with 2 Parameters
Method print with n parameters
Example 2
Overloads with different types of parameters
package demo.demo86; public class OverLoading2 { public void print(String a){ System.out.println("Parameter is String Type method"); } public void print(int a){ System.out.println("Parameter is int Type method"); } public void print(double a){ System.out.println("Parameter is double Type method"); } public static void main(String[] args) { OverLoading2 ol = new OverLoading2(); ol.print("a"); ol.print(1); ol.print(3.14); } }
function:
The parameter is a String type method
Parameter is an int type method
The parameter is a method of type double
Example 3
The type and number of parameters are mixed overloads.
package demo.demo86; public class OverLoading2 { public void print(String a){ System.out.println("Parameter is String Type method"); } public void print(int a){ System.out.println("Parameter is int Type method"); } public void print(int a,int b){ System.out.println("2 The first parameter is int Type method"); } public void print(double a){ System.out.println("Parameter is double Type method"); } public static void main(String[] args) { OverLoading2 ol = new OverLoading2(); ol.print("a"); ol.print(1); ol.print(1,2); ol.print(3.14); } }
function:
The parameter is a String type method
Parameter is an int type method
The two parameters are int type methods
The parameter is a method of type double
Example 4
If you want to overload through different return values, you will get a compilation error, which is not allowed.
Example 5
Can the main method be overloaded? The answer is yes, but only
public static void main(String[] args)
It will be recognized as an entry method by the virtual machine, and others are only used as ordinary methods.
package demo.demo86; public class OverLoading4 { public static void main(String[] args) { System.out.println("main String[]"); } public static void main(String a) { System.out.println("main String"); } }
Operation results:
main String[]
You can see that only public static void main(String[] args) has been executed. Of course, we have no problem calling it according to the normal method.
package demo.demo86; public class OverLoading4 { public static void main(String[] args) { System.out.println("main String[]"); //Call the normal method main(String) main("a"); } public static void main(String a) { System.out.println("main String"); } }
Operation results:
main String[]
main String
Summary
This section summarizes "Java method overloading". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning java with brother Xiao Ming, [pay attention to a wave] won't get lost.
Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!
Navigation
✪ introduction to Java white 200 case series directory index
◄ previous article 85. Why use up transformation instead of directly creating subclass objects
► next article to be updated
Popular column recommendation
1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory