方法重载演示


public class DemoTest {
// add(int,int)方法签名=方法名+参数列表
// 在Java中不可能出现方法签名相同的两个方法
public int add(int a, int b) {
System.out.println("返回int类型");
return a + b;
}
//add(short,short)
public int add(short a, short b) {
System.out.println("返回short类型");
return a + b;
}
// add(double,double)
public double add(double a, double b) {
System.out.println("返回double类型");
return a + b;
}

// add(long,long)
public long add(long a, long b) {
System.out.println("返回long类型");
return a + b;
}

public static void main(String[] args) {
DemoTest demo=new DemoTest();
//demo.add(2.0, 3.0);//add(double,double)
byte b1=2;
byte b2=3;
//就近原则,调用类型最近的方法
demo.add(b1, b2);//add(byte,byte)
}
}

原文地址:https://www.cnblogs.com/fxx0129-nn/p/6853400.html