A method is a piece of code used to complete a specific function, similar to functions in other languages.
Methods are used to define the behavioral characteristics and functional implementation of this class or instances of this class. Methods are abstractions of the behavioral characteristics of classes and objects. Methods are very similar to functions in procedure-oriented programming. In process orientation, function is the most basic unit, and the entire program is composed of function calls. In object-oriented, the basic unit of the entire program is a class, and methods are subordinate to classes and objects.
[Modifier 1 Modifier 2 …] Return value type method name (formal parameter list) {
Java statement;… ; … … }
Object name.Method name (actual parameter list);
Formal parameters: Used to receive data incoming from the outside when the method is declared.
Actual parameters: the data actually passed to the method when calling the method.
Return value: The data returned by the method to the environment that called it after completion of execution.
Return value type: The data type of the return value agreed in advance. If there is no return value, it must be explicitly specified as void.
Note: Everything in Java is passed by value
For example: we want to print a number from 1 to n. The traditional writing method is written in the main method, but when there are many When there is a value, you have to write multiple for loops, so the code becomes more repetitive.
public class TestCode02 { public static void main(String[] args) { int n1 = 10; for (int i = 1; i <= n1; i++) { System.out.print(i + " "); } System.out.println(); //当有多个n时,都要每次写一遍for循环 int n2 = 13; for (int i = 1; i <= n2; i++) { System.out.print(i + " "); } System.out.println(); int n3=20; //for... } }
We extract the same code and put it in a method, so that we can call this method every time without having to write the same code every time
public class TestCode02 { public static void main(String[] args) { int n1 = 10; printNnums(n1); int n2=12; printNnums(n2); int n3=14; printNnums(n3); } public static void printNnums(int n){ for (int i = 1; i <= n; i++) { System.out.print(i + " "); } System.out.println(); } }
In this way, we have multiple n, just call the method once
1. The method is: extract the specific function to form a code snippet. This code snippet is what we call a method
2. Methods and methods are in a parallel relationship, so the methods we define cannot be written into the main method
3. Method definition –> Format:
through ’ s ’ s through ’ through through through through through through use through through through through through through through ‐ ‐ ‐ ‐ ‐ to, and Code reusability
5. Summary method definition format:
Modifier:
public staticMethod return value type: The data type corresponding to the method's return value
,
short,long
,float
,double
,char
,boolean
) It can also be a reference data type Method name: Know the meaning of the name, the first letter is lowercase, and the rest follow camel case naming, eg: addNum, generally try to use English for naming
method at the call location has no return value: return can be omitted, and the return value type of the method is: void
When will there be a return value? When there is no return value? –>Look at the requirements
What is method overloading
Different meanings: different formal parameter types, number of formal parameters, and different order of formal parameters
只有形参的名称不同,不构成方法的重载;(如:int add(int a){}与int add(int b){}不构成方法重载)
public class TestCode03 { public static void main(String[] args) { add(7,8); add(1.02,2.03); add(1,3,5); add(1,4,6,9); } //定义一个int型两数相加 public static void add(int a,int b){ System.out.println(a+"+"+b+"="+(a+b)); } //定义一个double类型的两数相加 public static void add(double a,double b){ System.out.println(a+"+"+b+"="+(a+b)); } //定义一个三个数相加 public static void add(int a,int b,int c){ System.out.println(a+"+"+b+"+"+c+"="+(a+b+c)); } //四数相加 public static void add(int a,int b,int c,int d){ System.out.println(a+"+"+b+"+"+c+"+"+d+"="+(a+b+c+d)); } }
方法的重载:在同一个类中,方法名相同,形参列表不同的多个方法,构成了方法的重载。
方法的重载只跟:方法名和形参列表有关,与修饰符,返回值类型无关。
注意:形参列表不同指的是什么?
(1)个数不同
add() add(int num1) add(int num1,int num2)
(2)顺序不同
add(int num1,double num2) add(double num1,int num2)
(3)类型不同
add(int num1) add(double num1)
The above is the detailed content of Java method definition, calling and overloading methods. For more information, please follow other related articles on the PHP Chinese website!