Java方法(Java Procedure)

方法定义

格式:

public static void 方法名( ) {
//方法体
}

public static void 方法名( ) {
//方法体
}

方法调用:

方法名();

方法名();

注:

  • 方法必须先定义后调用,否则程序将报错

带参方法定义

格式:

public static void 方法名 (参数) {……}

public static void 方法名 (参数) {……}

格式(单个参数):

public static void 方法名(数据类型 变量名) {……}

public static void 方法名(数据类型 变量名) {……}

格式(多个参数):

public static void 方法名(数据类型 变量名1, 数据类型 变量名2, …… ) {……}

public static void 方法名(数据类型 变量名1, 数据类型 变量名2, …… ) {……}

注:

  • 方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错
  • 方法定义时,多个参数之间使用逗号(,)分隔

带参方法调用

格式:

方法名(参数)

方法名(参数)

格式(单个参数):

方法名(变量名);

方法名(变量名);

格式(多个参数):

方法名(变量名1,变量名2,……);

方法名(变量名1,变量名2,……);

注:

  • 方法调用时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错

形参和实参

形参:方法定义中的方法

等同于变量定义格式,例如:int i

实参:方法调用中的参数

等同于使用变量或常量,例如1、i

带返回值方法定义

格式:

public static 数据类型 方法名(参数){
//方法体
return 数据;//数据需要和数据类型相匹配
}

public static 数据类型 方法名(参数){
//方法体
return 数据;//数据需要和数据类型相匹配
}

注:

  • 方法定义时return后面的返回值与方法定义上的数据类型要匹配,否则程序将报错

带返回值方法调用

格式:

数据类型 变量名 = 方法名(参数);

数据类型 变量名 = 方法名(参数);

方法注意事项

  • 方法不能嵌套定义
  • void表示无返回值,可以省略return,也可以单独的书写return,后面不加数据(return;)

方法重载(Overload)

定义:

方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载

多个方法在同一个类中
多个方法具有相同的方法名
多个方法的参数数量不同、类型不同或者数量不同
返回类型可以相同也可以不同(故与返回值无关)

方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载

  • 多个方法在同一个类中
  • 多个方法具有相同的方法名
  • 多个方法的参数数量不同、类型不同或者数量不同
  • 返回类型可以相同也可以不同(故与返回值无关)

特点:

重载仅对应方法的定义,与方法的调用无关,调用方式参照标准格式
重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值来判定两个方法是否相互构成重载

  • 重载仅对应方法的定义,与方法的调用无关,调用方式参照标准格式
  • 重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值来判定两个方法是否相互构成重载

重载举例:

//求两个int类型数的和
public static int sum(int a, int b) {
    return a + b;
}
//求两个double类型数的和
public static double sum(double a, double b) {
    return a + b;
}
//求三个int类型数的和
public static int sum(int a, int b, int c) {
    return a + b + c;
}

方法参数传递(基本类型)

对于基本数据类型,,形式参数的改变,不影响实际参数的值

方法参数传递(引用类型)

对于引用类型的参数,形式参数的改变,影响实际参数的值

————————

Method definition

Format:

Public static void method name (){
//Method body
}

Public static void method name (){
//Method body
}

Method call:

Method name ();

Method name ();

Note:

  • Method must be defined before calling, otherwise the program will report wrong.

Definition of method with parameters

Format:

Public static void method name (parameter) {…}

Public static void method name (parameter) {…}

Format (single parameter):

Public static void method name (data type variable name) {…}

Public static void method name (data type variable name) {…}

Format (multiple parameters):

Public static void method name (data type variable name 1, data type variable name 2,…) {…}

Public static void method name (data type variable name 1, data type variable name 2,…) {…}

Note:

  • When defining a method, the data type and variable name in the parameter cannot be missing. If any program is missing, an error will be reported
  • When defining a method, multiple parameters are separated by commas (,)

Method call with parameters

Format:

Method name (parameter)

Method name (parameter)

Format (single parameter):

Method name (variable name);

Method name (variable name);

Format (multiple parameters):

Method name (variable name 1, variable name 2,…);

Method name (variable name 1, variable name 2,…);

Note:

  • When calling a method, the number and type of parameters must match the settings in the method definition, otherwise the program will report an error

Formal and argument

Formal parameter: Method in method definition

Equivalent to variable definition format, for example: int i

Arguments: parameters in method calls

Equivalent to using variables or constants, such as 1, i

Method definition with return value

Format:

Public static data type method name (parameter){
//Method body
Return data// The data needs to match the data type
}

Public static data type method name (parameter){
//Method body
Return data// The data needs to match the data type
}

Note:

  • When defining a method, the return value after return should match the data type on the method definition, otherwise the program will report an error

Method call with return value

Format:

Data type variable name = method name (parameter);

Data type variable name = method name (parameter);

Method precautions

  • Methods cannot be nested
  • Void indicates that there is no return value. You can omit return or write return separately without data (return;)

方法重载(Overload)

definition:

Method overloading refers to the relationship between multiple methods defined in the same class. Multiple methods that meet the following conditions constitute overloading with each other
Multiple methods in the same class
Multiple methods have the same method name
The number, type or quantity of parameters of multiple methods are different
The return type can be the same or different (so it has nothing to do with the return value)

Method overloading refers to the relationship between multiple methods defined in the same class. Multiple methods that meet the following conditions constitute overloading with each other

  • Multiple methods in the same class
  • Multiple methods have the same method name
  • The number, type or quantity of parameters of multiple methods are different
  • The return type can be the same or different (so it has nothing to do with the return value)

characteristic:

Overloading only corresponds to the definition of the method and has nothing to do with the method call. The call method refers to the standard format
Overloads only identify the names and parameters of methods in the same class, regardless of the return value. In other words, it is not possible to determine whether two methods constitute overloads with each other through the return value

  • Overloading only corresponds to the definition of the method and has nothing to do with the method call. The call method refers to the standard format
  • Overloads only identify the names and parameters of methods in the same class, regardless of the return value. In other words, it is not possible to determine whether two methods constitute overloads with each other through the return value

Overload example:

//求两个int类型数的和
public static int sum(int a, int b) {
    return a + b;
}
//求两个double类型数的和
public static double sum(double a, double b) {
    return a + b;
}
//求三个int类型数的和
public static int sum(int a, int b, int c) {
    return a + b + c;
}

Method parameter passing (basic type)

For basic data types, the change of formal parameters does not affect the value of actual parameters

Method parameter passing (reference type)

For parameters of reference type, the change of formal parameters will affect the value of actual parameters