首页 > 编程语言 > 详细

java方法的重载

时间:2020-05-27 21:25:13      阅读:34      评论:0      收藏:0      [点我收藏+]

多个方法的名称一样,但是参数列表不一样,多个参数

1参数个数不一样

2参数类型不一样

3参数的多类型的顺序不一样

 

无关的因素:

1参数的名称无关

 public static double sum(int c,int d)
和
  public static int sum(int a ,int b)
冲突

 

2与方法的返回值无关

 public static double sum(int a,int b)
和
  public static int sum(int a ,int b)
冲突

 

package damo01;

public class DemoMethodOverload {
    public static void main(String[] args) {
        System.out.println(sum(4,4));
        System.out.println(sum(1,1,1));

    }
    public static int sum(int a ,int b){
        return a+b;
    }

    public static int sum(int a ,int b,int c){
        return a+b+c;
    }
}

 

package damo01;

public class DemoMethodOverload {
    public static void main(String[] args) {
        byte a = 1;
        byte b = 2;
        System.out.println(isSame(a,b));
        System.out.println(isSame((short)a,(short)b));
    }

    public static boolean isSame(byte a,byte b){
        System.out.println("bytebyte");
        boolean same;
        if (a == b){
            same = true;
        }else {
            same = false;
        }
        return same;
    }

    public static boolean isSame(short a ,short b){
        boolean same = a == b? true : false;
        System.out.println("shortshort");
        return same;
    }

    public static boolean isSame(int a, int b){
        return a == b;
    }

}

 

java方法的重载

原文:https://www.cnblogs.com/java-quan/p/12976486.html

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