简体   繁体   English

重载方法

[英]Overloading methods

I saw below question posted on this site. 我看到以下问题发布在此网站上。

"What happens when we pass int arguments to the overloading method having float as a parameter for one method and another having double param". "What happens when we pass int arguments to the overloading method having float as a parameter for one method and another having double param".

I thought I understood the concept and wrote this code: 我以为我理解了这个概念并编写了以下代码:

public  class TestClass {

    public static void main(String args[])
    {

        TestClass t=new TestClass();

        t.sum(1/4);
    }
    void sum(double d)
    {
        System.out.println("Double==="+d);
    }
    void sum(int i)
    {
        System.out.println("Integer==="+i);
    }
    void sum(short s)
    {
        System.out.println("Short==="+d);
    }
}

According to my understanding explained on this site (as mentioned above), I thought it will print Short===0 , but to my surprise it prints Integer===0 . 根据我对此站点上解释的理解(如上所述),我认为它将打印Short===0 ,但令我惊讶的是,它打印了Integer===0 Can any one explain this to me? 有人可以向我解释吗?

First of all, these are overloaded methods, not overridden methods. 首先,这些是重载方法,而不是重写方法。

1 and 4 are integers. 14是整数。 Therefore 1/4 is an integer division, returning 0 . 因此1/4是整数除法,返回0

Therefore, the method being called is sum(int i) . 因此,被调用的方法是sum(int i)

sum(short s) would never be called for an int parameter, since that would require a narrowing primitive conversion (JLS 5.1.3) , that may cause data loss, and is not allowed in method invocation conversion (JLS 5.3) . sum(short s)永远不会为int参数调用,因为这将需要变窄的原始转换(JLS 5.1.3) ,这可能会导致数据丢失,并且在方法调用转换(JLS 5.3)中是不允许的。 Such a conversion can be done with an explicit cast. 这种转换可以通过显式强制转换完成。

If you remove the int version, sum(double d) would be called, and if you remove the double version, the code won't compile. 如果删除int版本,则将调用sum(double d) ,并且如果删除double版本,则代码将无法编译。

In order to call the short version, you must cast the parameter to short : 为了调用简短版本,必须将参数强制转换为short:

t.sum ((short)(1/4));

If you don't explicitly tell the compiler what are the types of 1 and 4 , it assumes they are of type int . 如果您没有明确告诉编译器14的类型是什么,则假定它们的类型为int Then, / operator will apply integer division and will produce another int (which will be 0 .) 然后, /运算符将应用整数除法,并将产生另一个int (将为0

After that, the method with the most specific to integer parameter type will be invoked. 之后,将调用最特定于整数参数类型的方法。 In your case, this will be sum(int i) . 在您的情况下,这将是sum(int i)

If you want to invoke some of the other overloaded methods, you will have to explicitly: 如果要调用其他一些重载方法,则必须显式:

  • do a cast. 做一个演员。 For example, sum((short) (1/4)); 例如sum((short) (1/4)); will invoke sum(short s) due to the cast. 由于强制转换将调用sum(short s)
  • point the type of the operands. 指出操作数的类型。 For example, sum(1d/4) will invoke sum(double d) , since 1d/4 will result to double 例如, sum(1d/4)将调用sum(double d) ,因为1d/4将导致double

For integer number, the type int is a default choice. 对于整数, int类型是默认选择。 So, although 1 and 4 can be defined as both int or short , since you did not defined anything, the compiler identified 1 and 4 as int and therefore it entered into the function for 1/4 division (0), which took the parameter int . 因此,尽管1和4都可以定义为intshort ,但由于您没有定义任何内容,编译器将1和4标识为int ,因此它进入了1/4除法(0)的函数,该函数采用了参数int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM