Java方法重载的区分

测试程序代码:
class Computer {
    private int x;

    public Computer(){
        this(10);
    }

    /**
     * 构造方法重载
     * @param x
     */
    public Computer(int x){
        this.x=x;
    }
    /**
     * 根据参数个数不同的方法重载
     * @param a
     * @param b
     * @return
     */
    public  int max(int a,int b){
        return max(a,b,Integer.MIN_VALUE);//Integer.MIN_VALUE是int类型中最小数-2147483648
    }
    public  int max(int a,int b,int c){
        int max=a;
        if (max<b) max=b;
        if (max<c) max=c;
        return max;
    }
    /**
     * 根据参数类型不同方法重载
     * @param x
     * @return
     */
    public int add(int x){
        return x;
    }
    public float add(float x){
        return x;
    }

    /**
     * 测试方法的返回值是否可以作为重载的标准
     * @return
     */
//    public int returnValue(){
//        return 5;
//    }
//    public float returnValue(){
//        return 5;
//    }
//    编译器报错,returnValue方法已经存在。很明显根据返回值类型是不可以判断方法是否重载的。
}
class OverLoading{
    public void test(){
        Computer computer=new Computer();
        new Computer(100);
        int max_2=computer.max(10,11);
        int max_3=computer.max(10,29,33);
        int tmp1=computer.add(10);
        float tmp2=computer.add(10f);
    }
}
结论:判断方法(函数)重载的依据是参数个数的不同和参数类型的不同,根据返回值类型的不同是不可以判断方法重载。

关联博客(博客园):http://www.cnblogs.com/qikeyishu/p/9013606.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值