重载方法优先级
我有一个名为 Element 的基类。其他一些类(如 Label 和 Image)都扩展了该类。
我现在有一个具有以下方法
public class Dispatcher {
public static AbstractPropertyEditor<Label> createEditor(Label e) {
...
}
public static AbstractPropertyEditor<Element> createEditor(Element e) {
...
}
}
的调度类:如果现在我有一个 Label 实例(扩展 Element)并且我想将其传递给 createEditor()
,为什么是最通用的方法(第二个)打电话?调用最具体的方法(createEditor(Label e)
)不是很正常吗?
我绝对需要带有 Element-param 的方法,以便“捕获”所有那些 a) 实现 Element 但在此调度类中没有自己的特定方法的类。
我正在使用 Java 6,如何“修复”这?
编辑:好吧,我不得不承认这根本与泛型无关。但这是我第一次遇到它的地方。
谢谢和问候
I have a base-class called Element. Some other classes (like Label and Image) both extend this class.
I now have a dispatching class having the following methods:
public class Dispatcher {
public static AbstractPropertyEditor<Label> createEditor(Label e) {
...
}
public static AbstractPropertyEditor<Element> createEditor(Element e) {
...
}
}
If now I have an instance of Label (which extends Element) and I want to pass it to createEditor()
, why is the most generic method (the second one) called? Wouldn't it be normal that the most specific method (createEditor(Label e)
) is called?
I absolutely need the method with the Element-param in order to "catch" all those classes that a) implement Element but do not have their own specific method in this dispatching class..
I'm using Java 6, how to "fix" this?
Edit: Okay, I have to admit it's not at all about generics. But that's where I encountered it the first time.
thanks and regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不:
createEditor()
实现的Element
抽象类,Label
覆盖createEditor()< /代码>。
因此,您不需要静态实用程序,并且可以实现您的目标。
如果您需要
Element
成为一个接口,那么:createEditor()
定义为Element
的方法EditorFactory
接口DefaultEditorFactory
和ListEditorFactory
在
Element
的实现者中使用适当的工厂:具体的
EditorFactory
在初始化期间或通过某种依赖注入进行实例化。根据您的具体问题 - 这取决于您在那里编译的类型。如果您调用
createEditor(obj)
,它将取决于它是Element obj = ..
还是Label obj = ..
Why don't you:
Element
abstract class that provides a defaultcreateEditor()
implementationLabel
override thecreateEditor()
.Thus you won't need the static utilities and will achieve your goal.
If you need
Element
to be an interface, then:createEditor()
as methods ofElement
EditorFactory
interfaceDefaultEditorFactory
andListEditorFactory
use the appropriate factories in the implementors of
Element
:where the concrete
EditorFactory
is instantiated either during initialization or via some sort of dependecy-injection.As per your concrete question - it depends on what type you have compiled there. If you call
createEditor(obj)
it will depend whether it'sElement obj = ..
orLabel obj = ..
这实际上与泛型无关,而与方法重载有关。在Java中,调用的方法签名是在编译时确定的,而不是在运行时确定的,因此必须在运行时检查和转换。
因此,将其替换
为
:解决此问题的另一种(更标准/更好)方法是让 createEditor(Element) 方法检查类型并使用强制转换调用子类型的正确重载方法。但是,如果您对声明的方法执行此操作,您的返回参数将会出现问题。
This really has little to do with generics, and everything to do with method overloading. In Java, the method signature called is determined at compile time, not at runtime, so you have to check and cast at runtime.
So replace this:
With this:
The other (more standard/better) way to fix this is to have the createEditor(Element) method check the type and call with a cast the correct overloaded method for subtypes. However, you will have an issue with your return parameters if you do that on the methods as declared.
来自 Java 语言规范:
From the Java Language Specification:
因为你可能正在做:
它是由编译器决定的。
Since you are probably doing :
It is determined by the compiler.
这是重载方法的示例。
即使运行时的实际对象是标签而不是元素,
选择要调用的重载方法(换句话说,
方法)不是在运行时动态决定的。引用类型(而不是对象类型)决定调用哪个重载方法!
因此
,在您的情况下,
另一方面,重写的方法调用发生在运行时,并且它取决于对象类型(换句话说,堆上实际实例的类型)
This is an example of overloaded methods.
Even though the actual object at runtime is a Label and not an Element, the
choice of which overloaded method to call (in other words, the signature of the
method) is NOT dynamically decided at runtime. The reference type (not the object type) determines which overloaded method is invoked!
Example
So, in your case
On other note, overridden method invocation happens at run time and it depends on Object type (in other words, the type of the actual instance on the heap)