问题
I keep getting an error saying that "call to super must be the first statement in the constructor".
The problem is that it is the first statement in my constructor.
public void CheckingAccountCustomer(int a){
super(n, p, b);
accountNo = a;
}
And here is my superclass for this as well.
public void customer(String n, int p, double b){
name = n;
pin = p;
balance = b;
}
What am I doing wrong here?
回答1:
This code
public void customer(String n, int p, double b){
is not a constructor. Constructors don't have return types, e.g. void. Assuming your class name is customer:
public customer(String n, int p, double b){
This applies to CheckingAccountCustomer too.
回答2:
public void CheckingAccountCustomer(int a){
That's not a constructor since it states it has a void return type. It's just a method of the same name as the class. Get rid of the return type.
public CheckingAccountCustomer(int a){
回答3:
public void CheckingAccountCustomer(int a)
This is a method not a constructor, since it has a return type.
回答4:
The constructor is used to create an instance of that Class, so it make no sense if it will let the user to change the return type (it can be dangerous too). That's why constructors has no return type.
As others have already answered, remove the return type and it'll become a constructor.
回答5:
Constructors never return something (either void or Object type).
public void CheckingAccountCustomer(int a){
super(n, p, b);
accountNo = a;
}
thus is not a constructor.
来源:https://stackoverflow.com/questions/16407455/call-to-super-must-be-first-statement-in-the-constructor-but-it-is