问题描述
所以我有这个接口
public interface EventHandler< E extends EventObject>
{
public void handleEvent(E event);
}
我想为它派生类来处理事件,如下所示:
public class WorkingHandler实现EventHandler< MouseEvent>
{
public void handleEvent(MouseEvent event)
{
// handle event
}
}
上面的例子工作正常。但是编译器不会让我多次实现EventHandler,因为
非常令人沮丧的Type Erasure。
public class NotWorkingHandler实现EventHandler< MouseEvent>,EventHandler< KeyEvent>
{
public void handleEvent(MouseEvent event)
{
//处理鼠标事件
}
public void handleEvent(KeyEvent event)
{
//处理关键事件
}
}
几个问题...
- 为什么Java不允许类型擦除? IIRC它与Java的早期版本兼容,正确吗?
编辑:我的意思是为什么Java有Type Erasure (反其道而行之)
-
是否有任何形式的mod/extension更好的词)或编程语言,让我可以绕过类型擦除?因为我没有使用旧版本的Java,所以我不在乎我的代码是否与旧代码兼容。
在Java语言中是否有任何解决方法,我可以用它来绕过类型擦除? 如果不是,那么在我的程序中编写事件处理代码的其他方法是什么? 我不确定你对Java中Type Erasure的理解是正确的。正如所述,您的泛型类的类型在 - 我认为你是对的Java实现了类型擦除以实现与以前版本语言的兼容性。
- 不,我认为没有mod。
- 不,没有解决方法(在编译时删除类型),但可以使用反射。
我认为一个更简单的解决方案应该是这样的:
public class WorkingHandler implements EventHandler<的EventObject>
{
public void handleEvent(EventObject event)
{
//处理事件
if(event instanceof MouseEvent)
handleMouseEvent((MouseEvent)事件) ;
...
}
...
private void handleMouseEvent(MouseEvent event)
{
// handle老鼠
}
}
So I have this interface
public interface EventHandler<E extends EventObject> { public void handleEvent(E event); }
and I want to derive classes for it that handle events, like so:
public class WorkingHandler implements EventHandler<MouseEvent> { public void handleEvent(MouseEvent event) { //handle event } }
Above example works fine. But compiler won't let me implement EventHandler multiple times, due to theextremely frustrating Type Erasure.
public class NotWorkingHandler implements EventHandler<MouseEvent>, EventHandler<KeyEvent> { public void handleEvent(MouseEvent event) { //handle mouse event } public void handleEvent(KeyEvent event) { //handle key event } }
Several questions...
- Why does Java not allow Type Erasure? IIRC it is something to do with being compatible with earlier versions of Java, correct?
EDIT: I mean why does Java have Type Erasure (had it the other way around)
Is there any sort of "mod"/"extension" (for lack of a better word) or programming languages that allow me to get around Type Erasure? As I am not using older versions of Java I don't care about my code being compatible with older code.
Are there any workarounds, within the Java language, that I can use to get around Type Erasure?
If no, what are some alternate ways to code event handling in my program that keeps the compiler happy?
解决方案I'm not sure that your understanding of Type Erasure in Java is correct. As explained here, the type of your generic class is lost at runtime.
In response to your questions:
- I think you are right that Java implements type erasure to enable compatibility with previous versions of the langauge.
- No I don't think there is a "mod."
- No there are no workarounds (types are removed at compile time), but you can use reflection.
- See below.
I think a simpler solution would be to so something like this:
public class WorkingHandler implements EventHandler<EventObject> { public void handleEvent(EventObject event) { // handle event if (event instanceof MouseEvent) handleMouseEvent((MouseEvent) event); ... } ... private void handleMouseEvent(MouseEvent event) { // handle mice } }
这篇关于类型擦除丑陋的头部,如何规避?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
回复您的问题: