In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "the definition, call and overloading method of Java method method". In daily operation, I believe many people have doubts about the definition, call and overloading method of Java method method. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "definition, call and overloading method of Java method method". Next, please follow the editor to study!
Definition and invocation of method what is a method
A method is a code snippet used to perform a specific function, similar to a function in other languages (function).
Method is used to define the behavioral characteristics and functional implementation of the class or an instance of the class. A method is an abstraction of the behavioral characteristics of classes and objects. The method is very similar to a procedure-oriented function. In the process-oriented process, the function is the most basic unit, and the whole program is composed of function calls. In object-oriented, the basic unit of the whole program is the class, and the method is subordinate to the class and object.
Declaration format of the method
[modifier 1 modifier 2.] Returns the value type method name (formal parameter list) {
Java statement; … ... ... }
The way the method is called
Object name. Method name (argument list)
A detailed description of the method
Formal parameter: used to receive incoming data when the method is declared.
Argument: the data actually passed to the method when the method is called.
Return value: the method returns data to the environment in which it is called after execution.
Return value type: the pre-agreed data type of the return value. If there is no return value, it must be specified as void.
Note: everything in Java is value passing.
For example: we want to print the number of 1 main, the traditional writing method is written in the main method, but when there are multiple values, we have to write multiple for loops, so the code is more repetitive.
Public class TestCode02 {public static void main (String [] args) {int N1 = 10; for (int I = 1; I passed in according to the need of the formal parameter
Method body: specific business logic code
The value returned by the return method:
Method if there is a return value: the return+ method returns the value, returning the return value to the calling place of the method
If the method does not return a value: return can be omitted, and the return type of the method is: void
When is there a return value and when is there no return value? -> look at the demand
6. What do you need to pay attention to when defining a method?
How to write the formal parameter list: define several parameters, what types are they-> uncertainties we will use as formal parameters of the method
Does the method need to return a value, and if so, what is the type of return value?
7. What do you need to pay attention to when calling the method?
How to pass in the actual parameters: several parameters and what type of
Whether the method has a return value to be accepted
Method overloading what is method overloading
Method overloading means that multiple methods with the same name but different parameters can be defined in a class. When called, the corresponding method is automatically matched according to different parameters.
Pay attention to the essence: the overloaded method is actually a completely different method, but with the same name!
Conditions for constituting method overloading
Different meanings: the type, number and order of formal parameters are different.
Only different return values do not constitute method overloading; (for example: int add (int aline int b) {} and void add (int aline int b) {} do not constitute method overload)
Only the name of formal parameter is different, it does not constitute method overload; (for example, int add (int a) {} and int add (int b) {} do not constitute method overload)
Public class TestCode03 {public static void main (String [] args) {add (7Piag8); add (1.02 pencils 2.03); add (1pje 3 pens 5); add (1pas 4 pas 6 pas 9);} / / define an int type plus two numbers public static void add (int areint b) {System.out.println (a + "+" + b + "=" + (aqb) } / / define the addition of two numbers of double type public static void add (double amageddoub) {System.out.println (a + "+" + b + "=" + (aqb));} / / define a sum of three numbers public static void add (int aline int bpenint int c) {System.out.println (a + "+" + b+ "+" + c + "=" + "(a+b+c) } / / the addition of quaternions public static void add (int arecine int breint c int d) {System.out.println (a + "+" + b + "+" + c + "+" + d + "=" + (a+b+c+d));}}
Summary
Method overloading: in the same class, multiple methods with the same method name and different formal parameter lists constitute the method overloading.
Method overloading is only related to the method name and formal parameter list, and has nothing to do with modifiers and return value types.
Note: what do different parameter lists mean?
(1) the number is different
Add () add (int num1) add (int num1,int num2)
(2) different order
Add (int num1,double num2) add (double num1,int num2)
(3) different types
Add (int num1) add (double num1)
At this point, the study on the definition, invocation and overloading of Java method method is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.