简体   繁体   English

Java重载的奇怪行为

[英]Strange behavior with java overloading

i've some problems with java overloading and dynamic parameters.. 我有一些java重载和动态参数的问题..

import java.lang.*;

public class Program
{
     public static void main(String []args){
        testOverloading("Test string");
        testOverloading(new Object());
        testOverloading( true ? "Must be a string" : new Object());
     }

     public static void testOverloading(String test) {
         System.out.println("it's a string");
     }

     public static void testOverloading(Object test) {
         System.out.println("it's an object");
     }

}

running this code the java assume that "true ? "Must be a string" : new Object()" is an object and not a string..and the output is the follow: 运行此代码,java假设“true?”必须是一个字符串“:new Object()”是一个对象而不是一个字符串..输出如下:

it's a string
it's an object
it's an object

is there a solution/explaination for this kind of issue? 对于这类问题有解决方案/解释吗?

Update: 更新:

i tried also using a different approach: 我也尝试过使用不同的方法:

changing: 变化:

testOverloading( true ? "Must be a string" : new Object());

in

testOverloading( true ? "Must be a string" : new Program());

and

public static void testOverloading(Object test) {

in

public static void testOverloading(Program test) {

and the output is: 输出是:

error: no suitable method found for testPoly(Object)

so i've to assume that it's a compiler limitation with parameters that use single-line condition 所以我假设它是一个编译器限制,参数使用单行条件

in fact using normal the output is right: 实际上使用normal是正确的输出:

    if (true)
        testOverloading("Must be a string");
    else
        testOverloading(new Object());

output: it's a string

true ? "Must be a string" : new Object() true ? "Must be a string" : new Object() should have a single type of return. true ? "Must be a string" : new Object()应该有一种类型的返回。 In this case, the compiler will choose the highest class in the class hierarchy for the elements being returned, which is Object . 在这种情况下,编译器将为返回的元素选择类层次结构中的最高类,即Object

In ternary operator the return type of both the expression should be same. 在三元运算符中,表达式的返回类型应该相同。 But the compiler chooses the highest class hierarchy which is new object() in case of any conflict. 但是编译器选择最高级别的层次结构,如果发生任何冲突,它就是new object()

The Java docs says: Java文档说:

The type of a conditional expression is determined as follows: 条件表达式的类型确定如下:

If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression. 如果第二个和第三个操作数具有相同的类型(可以是null类型),那么这就是条件表达式的类型。

There's no way to know if your ternary operator will return a string or an object in compiling time so the compiler chooses the type of the highest class in the hierarchy, which is object in this case. 无法知道您的三元运算符是否会在编译时返回字符串或对象,因此编译器会选择层次结构中最高类的类型,在本例中为对象。

You can better understand it by trying to assign the result of the ternary operator to a String variable, you will get a compilation error 您可以通过尝试将三元运算符的结果分配给String变量来更好地理解它,您将收到编译错误

Ex: 例如:

String result = true ? "Must be a string" : new Object(); // compiler error

It is not strange. 这并不奇怪。 It is because string is inherited from an object. 这是因为字符串是从对象继承的。 Since your return statement can be of type object or string it chooses object type. 由于return语句可以是object或string类型,因此它选择对象类型。 If you cast it to be a string you would see different behavior. 如果你把它变成一个字符串,你会看到不同的行为。

you need to do something like this: 你需要做这样的事情:

public class ReturnClass{
    Object object;
    ReturnType returnType;  


    public static Enum ReturnType {
        STRING,
        OBJECT   
    }
}

In an expression like statement, string constant would be constructed by compiler using StringBuilder. 在类似于语句的表达式中,字符串常量将由编译器使用StringBuilder构造。 In an example of true ? 在一个真实的例子? "Must be a string" : new Object(), turns to true ? “必须是一个字符串”:new Object(),转为true? new StringBuilder ("Must be a string") : new Object(). new StringBuilder(“必须是一个字符串”):new Object()。 Hence there was no match for StringBuilder, then it tries to typecast to parent default Object type. 因此,StringBuilder没有匹配,然后它尝试将类型转换为父默认对象类型。

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

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