简体   繁体   English

Java中是否可以实现动态重载解析?

[英]Is dynamic overload resolution possible in Java?

So I have a class of overloaded methods like this: 所以我有一类重载方法,如下所示:

class Foo {
    public void test(Object value) {
        ...
    }

    public void test(String value) {
        ...
    }
}

I need to pass a property value of a bean to one of these methods depending on its type, but I don't know the actual property type until the runtime. 我需要根据其类型将bean的属性值传递给其中一个方法,但在运行时之前我不知道实际的属性类型。 eg 例如

public void run(Object bean, String propertyName) {
    Foo foo = new Foo();
    foo.test(PropertyUtils.getProperty(bean, propertyName));
}

BTW, PropertyUtils.getProperty() is a helper method that returns a value of the specified property on a bean. BTW, PropertyUtils.getProperty()是一个辅助方法,它返回bean上指定属性的值。 PropertyUtils.getProperty() returns an Object, so that test(Object value) will be always called and the actual property type will be ignored. PropertyUtils.getProperty()返回一个Object,因此将始终调用test(Object value)并忽略实际的属性类型。

I can figure out the propery type in the runtime, even if its value is null. 我可以在运行时中找出propery类型,即使它的值为null。 Is there such a thing as dynamic casting in Java? 在Java中有动态转换这样的东西吗? If not, is there a way to have an overloaded method with the correct parameter type called? 如果没有,是否有一种方法可以使用正确的参数类型调用重载方法?

Overriding is what has dynamic binding in Java. 覆盖是Java中的动态绑定。 Overloading has static binding, and which function is called is determined at compile time, not at runtime. 重载具有静态绑定,并且调用哪个函数是在编译时确定的,而不是在运行时确定的。 See this SO question. 看到这个问题。

Therefore you can't use overloading for run time selection of methods. 因此,您不能使用重载进行运行时选择方法。 Suggest you use one of the other OOP design patterns in java, or at least instanceof : 建议你在java中使用其他OOP设计模式之一,或者至少是instanceof

public void dispatch(Object o)
{
   if (o instanceof String)
       handleString((String)o);
   else if (o instanceof File)
       handleFile((File)o);
   else
       handleObject(o);
}

这是访客模式的工作。

Overloaded method resolution happens at compile time in Java. 在Java中编译时发生重载的方法解析。 You'll have to do the resolution yourself (a switch, if-then-else ladder or table-lookup), or find a different pattern that can be implemented in Java. 您必须自己完成解决方案(交换机,if-then-else梯形图或表查找),或者找到可以用Java实现的不同模式。

I'm not sure you want an answer to this question. 我不确定你想要回答这个问题。 You're interested in setting up a situation where 你有兴趣建立一个情况

test((String)x)

does not do the same thing as 不做同样的事情

test((Object)x)

For x which are String s. 对于xString s。 This is a Bad Idea™ and will just lead to lots of confusion. 这是一个坏主意™,只会导致很多混乱。 Use a different method name if you really want different behavior. 如果您真的想要不同的行为,请使用不同的方法名称。

Just make test(Object x) dispatch to test(String x) if x is a String , then you don't need to worry about which test method gets called. 如果x是一个String ,只需使test(Object x) dispatch to test(String x) ,然后你就不必担心调用哪个test方法了。

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

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