-
方法重载概念
方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载
-
多个方法在同一个类中
-
多个方法具有相同的方法名
-
多个方法的参数不相同,类型不同或者数量不同
-
-
注意:
-
重载仅对应方法的定义,与方法的调用无关,调用方式参照标准格式
-
重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值来判定两个方法是否相互构成重载
-
标题代码实现:
package com.acot.test.demo;
public class method {
public static int max(int a,int b){
return a>b?a:b;
}
public static double max(double a,double b){
return a>b?a:b;
}
public static double max(double a,double b,double c){
double max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
public static void main(String[] args){
int intMax = max(5, 10);
System.out.println("两个整数中的最大值: " + intMax);
double doubleMax1 = max(3.5, 2.8);
System.out.println("两个双精度浮点数中的最大值: " + doubleMax1);
double doubleMax2 = max(1.2, 5.6, 3.9);
System.out.println("三个双精度浮点数中的最大值: " + doubleMax2);
}
}