super() 不允许我使用参数调用超级构造函数
我想调用带有两个参数的超类构造函数,因此我调用 super(arguments),但编译器说:
“类 Person 中的构造函数 Person 不能应用于给定类型;
必需:无参数
找到:Java.lang.String、int
原因:实际参数列表和正式参数列表的长度不同”
我不知道为什么会发生这种情况。对 super 的调用是第一行。参数匹配(但由于某种原因不匹配编译器)。有人知道发生了什么吗?
顺便说一句,我正在使用 NetBeans。
编辑:我卸载了 NetBeans,安装了 Eclipse,现在一切正常,我不知道,也永远不会知道为什么 NetBeans 给我一个错误。为了你的帮助。
public class Person
{
private String name;
private int age;
public Person() { name = " "; age = 0; }
public Person(String nm, int ag) { name = nm; age = ag; }
}
public class BaseballPlayer extends Person
{
private int homeruns;
private double bat_avg;
BaseballPlayer()
{
super();
homeruns = 0;
bat_avg = 0;
}
BaseballPlayer(String name, int age, int hr, double bavg)
{
super(name, age); // THIS CAUSES AN ERROR. I DON'T KNOW WHY
homeruns = hr;
bat_avg = bavg;
}
}
I want to call the super class' constructor that takes two arguments, so I call super(arguments), but the compiler says:
"Constructor Person in class Person cannot be applied to given types;
required: no arguments
Found: Java.lang.String, int
reason: actual and formal argument lists differ in length"
I have no clue why this is happening. The call to super is the first line. The arguments match (but not to the compiler for some reason). Does anyone know what's going on?
I'm using NetBeans, by the way. The latest version.
EDIT: I uninstalled NetBeans, installed Eclipse, and now everything works perfectly. I don't know, nor will I ever know, why NetBeans gave me an error. Thanks everyone for your help.
public class Person
{
private String name;
private int age;
public Person() { name = " "; age = 0; }
public Person(String nm, int ag) { name = nm; age = ag; }
}
public class BaseballPlayer extends Person
{
private int homeruns;
private double bat_avg;
BaseballPlayer()
{
super();
homeruns = 0;
bat_avg = 0;
}
BaseballPlayer(String name, int age, int hr, double bavg)
{
super(name, age); // THIS CAUSES AN ERROR. I DON'T KNOW WHY
homeruns = hr;
bat_avg = bavg;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
顺便说一句,恕我直言,这样做是更好的形式:
这意味着一切都通过一个构造函数。如果您必须在构造函数中添加任何内容,则只有一处可以进行更改。
btw, IMHO it's better form, to do this:
This means everything goes through the one constructor. If you have to add anything into your constructor, you have only one place to do change.
我能想到的上面的代码在单独的文件中不起作用的唯一原因是如果发生以下情况:
Person
类Person
类有一个不同的构造函数BaseballPlayer
类的文件导入了该类 结论- 检查您的导入语句(当这些类位于单独的文件中时)。
顺便说一句,当
BaseballPlayer
位于同一文件中时,从BaseballPlayer
中删除public
与您想要实现的目标不同 -BaseballPlayer
将是包私有访问,而不是公共访问。编辑:
啊,因为我不熟悉 NetBeans:
Person
类没有自动编译?The only reason I can think of for the code above to not work when in separated files is if the following happened:
Person
classPerson
class has a different constructorBaseballPlayer
class imported that one insteadConclusion - check your import statements (when the classes are in separate files).
By the way, removing
public
from theBaseballPlayer
when they are in the same file is different then what you are trying to achieve -BaseballPlayer
will be packag-private access, instead of public.EDIT:
ah, since I am not familiar with NetBeans:
Person
class is not automatically compiled?没有理由给你一个错误。尝试进行干净的构建。
There's no reason for that to give you an error. Try doing a clean build.
我使用的是 NetBeans 7.0.1,它在 JDK 6 下作为单个文件、两个单独的文件以及单独包中的两个单独文件工作。
但是,我会尝试一件事:您应该将 BaseballPlayer 构造函数声明为公共。按照您现在编写的方式,它们是默认的安全设置,即包私有。除非您打算让 BaseballPlayer 对象仅在该类所在的包中实例化,并且永远不会被子类化,否则您应该将其更改为 public。
您正在运行的操作系统上的 NetBeans 中可能存在错误,该错误不喜欢 BaseballPlayer 构造函数的默认可见性。
I am using NetBeans 7.0.1 and this works under JDK 6 as a single file and as two separate files as well as two separate files in separate packages for me.
However, there is one thing I would try: you should declare your BaseballPlayer constructors as public. The way you have it written now, they are the default security setting, which is package private. Unless you intend for your BaseballPlayer objects to only be instantiated form within the package the class is located and never be subclassed, you should really change it to public.
There might be a bug in NetBeans on the operating system that you're running that doesn't like the default visibility of the BaseballPlayer's constructor.
我不知道这是否能解决你的问题,但值得一试。现在,您的所有类都位于默认的未命名包中。 (这就是当你没有 package 语句时会发生的情况。)
不推荐这样做,因为会发生很多不好的事情。会发生什么不好的事情呢?我不太清楚,因为我总是按照建议将东西放在命名包中。尝试将您的类放入命名包中,看看问题是否仍然存在。
I do not know if this will solve ur problem, but it is worth a shot. Now, all your classes are in the default unnamed package. (This is what happens when you do not have a package statement.)
This is not recommende because a lot of bad things will happen. What bad things will happen? I don't know exactly because I always put things in a named package - as recommended. Try putting your classes in a named package and see if you still have the problem.
在查看该错误时,我的第一个想法是 Netbeans“保存时编译”设置已启用。虽然增量编译很多时候很方便,但它确实有时会出现一些棘手的错误,特别是当您开始更改并添加构造函数并扩展它们时。它偶尔会感到困惑并开始缓存旧版本的 .class 文件,因为它认为新版本无法编译。
[编辑:事实上,您说在将两个类拆分之前,您曾经将它们放在一个文件中,这一事实足以导致命名空间冲突,这可能会使其尝试针对错误的缓存版本进行编译]
作为故障排除步骤,关闭保存时编译,并关闭任何其他自动编译设置。然后进行清洁。然后手动检查目录(或项目目录)并确保 .class 文件确实消失了。然后再次编译。问题可能会自行消失。
如果这不起作用,我还会尝试命令行构建,因为这将有助于缩小问题是否是 netbeans 配置/缓存问题,或者您的代码中是否存在其他错误(就像您可能不存在)在您认为的目录中进行编译,或者您的文件名不太正确,或者 netbeans 不太挑剔的其他内容。
My first thought in looking at that error is that Netbeans "Compile on Save" setting is enabled. And while having incremental compilation is convienient a lot of times, it does have some finicky bugs from time to time, particularly when you start changing around and adding constructors and extending them. It occasionally gets confused and starts caching an old version of the .class file because it thinks the new one doesn't compile.
[Edit: The fact that you said you used to have the two classes in one file before you split them up is more than enough reason to cause a namespace collision that might make it try to compile against the wrong cached version]
As a troubleshooting step, turn off compile on save, turn off any other automatic compile settings as well. Then do a clean. Then go check the directory (or project directories) manually and make sure the .class files are really gone. And then compile again. The problem may just go away on its own.
If that doesn't work, I would also try a command-line build, because that would help narrow down whether the problem is a netbeans configuration/caching issue, or whether there's some other bug in your code (like maybe you're not compiling in the directory you think you are, or your filename isn't quite right or something else that the netbeans is less picky about.