java方法的重载

方法重载例子,两个方法功能相同,参数类型不同

class test{
    public static void add(int a, int b){
        System.out.println(a+b);
    }
    public static void add(double a, double b){
        System.out.println(a+b);
    }
}

public class Main {

    public static void main(String[] args)  {
        int a1 = 1;
        int b1 = 2;
        double a2 = 1.2;
        double b2 = 1.5;
        test.add(a1, b1);
        test.add(a2, b2);
    }
}

 int会向上提升

class test{

    public static void add(double a, double b){
        System.out.println(a+b);
        System.out.println("double");
    }
}

public class Main {

    public static void main(String[] args)  {
        int a1 = 1;
        int b1 = 2;
        test.add(a1, b1);
    }
}

如果向下,则要强制转换

class test{

   public static void add(int a, int b){
        System.out.println(a+b);
        System.out.println("int");
    }

}

public class Main {

    public static void main(String[] args)  {
        double a2 = 1.2;
        double b2 = 1.5;
        test.add((int)a2, (int)b2);
    }
}

如何区分方法是否重载了呢?

1.重载方法的参数个数不同。

2.重载方法的参数个数相同,但是数据类型不全相同。

3.重载方法的参数个数相同,数据类型相同,但是数据类型相同的顺序不同。(不推荐)

  即每个重载的方法都有一个独一无二的参数类型列表(个数,类型,顺序)。

  注意:不能以方法返回类型来判断是否重载。

虚拟机通过静态类型来确定重载方法

abstract class Human{

}

class Man extends Human{

}

class Women extends Human{

}

class test{
    public static void sayhello(Human human){
        System.out.println("hello,human");
    }

    public static void sayhello(Man man){
        System.out.println("hello,man");
    }

    public static void sayhello(Women women){
        System.out.println("hello,woman");
    }
}

public class Main {

    public static void main(String[] args)  {
        Human man = new Man();
        Human women = new Women();
        test.sayhello(man);
        test.sayhello(women);
    }
} 

在main中,man和women的静态类型为Human,java虚拟机在执行时,通过静态类型来确定应该调用哪一个重载方法。

结果如图:


如果找不到对应的重载方法,则如前面讲的,参数向上提升,找到最合适的重载方法。

class Overload{
    public static void sayHello(Object arg){
        System.out.println("hello object");
    }

    public static void sayHello(int arg){
        System.out.println("hello int");
    }

    public static void sayHello(long arg){
        System.out.println("hello long");
    }

    public static void sayHello(Character arg){
        System.out.println("hello Character");
    }

    public static void sayHello(char arg){
        System.out.println("hello char");
    }

    public static void sayHello(char... arg){
        System.out.println("hello char...");
    }

    public static void sayHello(Serializable arg){
        System.out.println("hello Serializable");
    }
}

public class Main {

    public static void main(String[] args)  {
        Overload.sayHello('a');
    }
}

    此例子中字符a,最合适的重载方法的优先级char>int>long>float>double>Character >Serializable>Object>char...  。可以通过依次注释掉验证。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值