java 指定类型时,javac“使用未经检查或不安全的操作”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14250064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javac "uses unchecked or unsafe operations" when the type is being specified
提问by Alvaro
The following code:
以下代码:
public void addGrillaListener(Stage stageToClose,Grilla listener)
{
GrillaHandler<WindowEvent> handy = new GrillaHandler<>(listener);
if(stageToClose!=null)
{
stageToClose.addEventHandler(WindowEvent.WINDOW_HIDDEN,handy);
}
}
causes the compiler to show that message. How can I avoid it?
导致编译器显示该消息。我怎样才能避免它?
Extra info:
额外信息:
- Grilla is a Stage interface
- GrillaHandler is a EventHandler subclass that takes a Grilla as a constructor parameter
- Using JDK 7, GrillaHandler<> is allowed
- The compiler message is rather unespecific - it states that this method uses unchecked or unsafe operations
- Stage is a class provided by oracle, it's part of javafx
- Grilla 是一个舞台界面
- GrillaHandler 是一个 EventHandler 子类,它将 Grilla 作为构造函数参数
- 使用 JDK 7,GrillaHandler<> 是允许的
- 编译器消息相当不明确 - 它指出此方法使用未经检查或不安全的操作
- Stage是oracle提供的一个类,它是javafx的一部分
GrillaHandler:
GrillaHandler:
public class GrillaHandler<T> implements EventHandler {
private Grilla win;
public GrillaHandler(Grilla win) {
this.win=win;
}
@Override
public void handle(Event t) {
win.loadTable();
}
}
Grilla:
格栅:
public interface Grilla {
public void loadTable();
}
回答by Edwin Dalorzo
Change code to
将代码更改为
public class GrillaHandler<T extends Event> implements EventHandler<T>{
//...
}
The JavaFX EventHandleris a paremeterized type. You are missing that one in your declaration of the GrillaHandler
. You are forced to provide a type argument in your class declaration or redeclare the type parameter, as you seem to require as per your declaration.
JavaFX EventHandler是参数化类型。您在GrillaHandler
. 您被迫在类声明中提供类型参数或重新声明类型参数,正如您的声明所要求的那样。