首页 > 其他 > 详细

方法重载演示

时间:2017-05-14 19:32:29      阅读:288      评论:0      收藏:0      [点我收藏+]


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

方法重载演示

原文:http://www.cnblogs.com/fxx0129-nn/p/6853400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!