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 h开发者_如何学Cave 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;
}
}
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:
- You have another
Person
class - That "other"
Person
class has a different constructor - Your file with
BaseballPlayer
class imported that one instead
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.
EDIT:
ah, since I am not familiar with NetBeans:
- perhaps the "original"
Person
class is not automatically compiled?
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.
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.
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. 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.
精彩评论