Java 7 中的类型错误
我有一个扩展 JDialog 的对话框类。此类中的一种方法是这样的:
public char getType()
{
return ((String)fileTypeCombo.getSelectedItem()).charAt(0);
}
其中 fileTypeCombo 是这样的:
JComboBox fileTypeCombo = new JComboBox(
new String[] { "Local", "Shared", "Remote" } );
当我尝试使用 Java 7 进行编译时,出现以下错误:
[javac] /home/satin/decodes-8.0/lrgs/gui/NetlistDialog.java:112: error: getType() in NetlistDialog cannot override getType() in Window
[javac] public char getType()
[javac] ^
[javac] return type char is not compatible with Type
它可以使用 Java 6 正常编译
。此致。
I have a dialog box class that extends JDialog. One method in this class is this:
public char getType()
{
return ((String)fileTypeCombo.getSelectedItem()).charAt(0);
}
where fileTypeCombo is this:
JComboBox fileTypeCombo = new JComboBox(
new String[] { "Local", "Shared", "Remote" } );
I am getting the following error when I attempt to compile using Java 7:
[javac] /home/satin/decodes-8.0/lrgs/gui/NetlistDialog.java:112: error: getType() in NetlistDialog cannot override getType() in Window
[javac] public char getType()
[javac] ^
[javac] return type char is not compatible with Type
It compiles fine with Java 6.
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 Window 类中添加了一个方法。
超类
Window
在 Java 7 中具有public Window.Type getType()
作为方法签名。您正在尝试重写该方法,但返回的是char
而不是Window.Type
对象,因此发生编译错误。在 Java 6 中该方法不存在 ,这样您就不会收到任何错误。
It is because of a method added to the Window class in Java 7.
The super class,
Window
, haspublic Window.Type getType()
for the method signature in Java 7. You are attempting to override that method, but are returning achar
instead of aWindow.Type
object, so a compilation error is occurring.In Java 6 that method doesn't exist, so you don't get any errors.