简体   繁体   English

super()不让我用参数调用超级构造函数

[英]super() not letting me call the super constructor with parameters

I want to call the super class' constructor that takes two arguments, so I call super(arguments), but the compiler says: 我想调用带有两个参数的超类'构造函数,所以我调用super(arguments),但编译器说:

"Constructor Person in class Person cannot be applied to given types; “类Person中的构造函数Person不能应用于给定类型;
required: no arguments 必需:没有参数
Found: Java.lang.String, int 找到: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. 对super的调用是第一行。 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. 顺便说一句,我正在使用NetBeans。 The latest version. 最新版本。

EDIT: I uninstalled NetBeans, installed Eclipse, and now everything works perfectly. 编辑:我卸载了NetBeans,安装了Eclipse,现在一切正常。 I don't know, nor will I ever know, why NetBeans gave me an error. 我不知道,也不会知道为什么NetBeans会给我一个错误。 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;
  }
 } 

btw, IMHO it's better form, to do this: 顺便说一句,恕我直言,这是更好的形式,这样做:

public class Person
    ...
    public Person() { 
        this("", 0); // No-args constructor calls the arg'd version with default values
    }

    public Person(String nm, int ag) {
        name = nm;
        age = ag;
    }
    ...
}

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. 如果必须在构造函数中添加任何内容,则只有一个地方可以进行更改。

The only reason I can think of for the code above to not work when in separated files is if the following happened: 我可以想到上面的代码在分隔文件中不起作用的唯一原因是如果发生以下情况:

  1. You have another Person class 你有另一个Person
  2. That "other" Person class has a different constructor 那个“其他” Person类有不同的构造函数
  3. Your file with BaseballPlayer class imported that one instead 你的BaseballPlayer类文件导入了那个

Conclusion - check your import statements (when the classes are in separate files). 结论 - 检查导入语句(当类在单独的文件中时)。

By the way, removing public from the BaseballPlayer 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. 顺便说一句,消除publicBaseballPlayer当他们在同一个文件是不同的,那么你想达到什么- BaseballPlayer将packag,私人的访问,而不是公众。

EDIT: 编辑:

ah, since I am not familiar with NetBeans: 啊,因为我不熟悉NetBeans:

  1. perhaps the "original" Person class is not automatically compiled? 也许“原始” Person类不会自动编译?

There's no reason for that to give you an error. 这没有理由给你一个错误。 Try doing a clean build. 尝试做一个干净的构建。

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. 我正在使用NetBeans 7.0.1,它在JDK 6下作为单个文件和两个单独的文件以及两个单独的文件在单独的包中工作。

However, there is one thing I would try: you should declare your BaseballPlayer constructors as public. 但是,有一件事我会尝试:你应该将你的BaseballPlayer构造函数声明为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. 除非您打算将BaseballPlayer对象仅在包中定义实例,而且该类定位并且永远不会被子类化,否则您应该将其更改为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. 您正在运行的操作系统上的NetBeans可能存在一个错误,它不喜欢BaseballPlayer构造函数的默认可见性。

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. 尝试将您的类放在命名包中,看看是否还有问题。

My first thought in looking at that error is that Netbeans " Compile on Save " setting is enabled. 我在查看该错误时的第一个想法是Netbeans“ Compile on Save ”设置已启用。 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. 它偶尔会混淆并开始缓存旧版本的.class文件,因为它认为新版本不会编译。

[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. 然后手动检查目录(或项目目录)并确保.class文件确实消失了。 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. 如果这不起作用,我也会尝试命令行构建,因为这将有助于缩小问题是否是netbeans配置/缓存问题,或者代码中是否存在其他错误(例如,您可能不是在您认为自己的目录中进行编译,或者您的文件名不是很正确或者netbeans不那么挑剔的其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM