用Short和int重载 [英] Overloading with Short and int

查看:135
本文介绍了用Short和int重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这段代码会打印int?

Why this code will print int?

public static void main(String[] args) {
    short s = 5;
    A(s);
}
public static void A(int a){
    System.out.println("int");
}

public static void A(Short a){
    System.out.println("short");
}


推荐答案

因为向上转换为 int 在Java 1.0版本中,并在5.0版本中添加了自动装箱。改变行为会破坏为旧版Java编写的代码。

Because upcasting to int was in version 1.0 of Java and auto-boxing was added in version 5.0. Changing the behaviour would break code written for older version of Java.

然而,这样的混合类型表明你的设计有问题,它只是你要去的东西在益智游戏中找到。 ;)

However, mixing types like this suggests there is something wrong with your design, its only something you are going to find in puzzlers. ;)

这篇关于用Short和int重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆