1. 程式人生 > >方法重載演示

方法重載演示

ati strong turn ble test print 方法名 ron byte


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)
}
}

方法重載演示