方法重载的小demo
方法的重载(overload)要求:1,同一个类中2,方法名必须相同3,方法的参数列表不同(1,参数的个数不同2,参数类型不同,但是参数名相同)
注:方法的重载与方法的返回值类型没有关系
package cao.com.duixiang;
public class OverloadTest {
public static void main(String[] args) {
Test test = new Test();
test.mOL(2);
test.mOL(2, 3);
test.mOL("cc");
QiuJie qj = new QiuJie();
//qj.max(2, 3);
System.out.println(qj.max(2, 3));
qj.max(2.1, 3.2);
qj.max(3.5, 3.32, 4.1);
}
}
class Test{
public int mOL(int a){
System.out.println(a*a);
return a*a;
}
public int mOL(int a,int b){
System.out.println(a*b);
return a*b;
}
public void mOL(String a){
System.out.println(a);
}
}
class QiuJie{
public int max(int a,int b){
int max;
max = a>b?a:b;
//System.out.println(max);
return max;
}
public double max(double a,double b){
double max = a>b?a:b;
System.out.println(max);
return max;
}
public double max(double a,double b,double c){
double max = (a>b?a:b)>c?(a>b?a:b):c;
System.out.println(max);
return max;
}
}