Java中的继承-“找不到符号构造函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/510784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inheritance in Java - "Cannot find symbol constructor"
提问by benwad
I'm working on a class that inherits from another class, but I'm getting a compiler error saying "Cannot find symbol constructor Account()". Basically what I'm trying to do is make a class InvestmentAccount which extends from Account - Account is meant to hold a balance with methods for withdrawing/depositing money and InvestmentAccount is similar, but the balance is stored in shares with a share price determining how many shares are deposited or withdrawn given a particular amount of money. Here's the first few lines (around where the compiler pointed out the problem) of the subclass InvestmentAccount:
我正在处理一个从另一个类继承的类,但是我收到一个编译器错误,提示“找不到符号构造函数 Account()”。基本上我想要做的是创建一个从 Account 扩展的类 InvestmentAccount - Account 旨在通过提取/存入资金的方法保持余额,而 InvestmentAccount 是类似的,但余额存储在股票中,股价决定如何给定特定数额的资金,许多股票会被存入或提取。这是子类 InvestmentAccount 的前几行(围绕编译器指出问题的地方):
public class InvestmentAccount extends Account
{
protected int sharePrice;
protected int numShares;
private Person customer;
public InvestmentAccount(Person customer, int sharePrice)
{
this.customer = customer;
sharePrice = sharePrice;
}
// etc...
The Person class is held in another file (Person.java). Now here's the first few lines of the superclass Account:
Person 类保存在另一个文件 (Person.java) 中。现在这是超类 Account 的前几行:
public class Account
{
private Person customer;
protected int balanceInPence;
public Account(Person customer)
{
this.customer = customer;
balanceInPence = 0;
}
// etc...
Is there any reason why the compiler isn't just reading the symbol constructor for Account from the Account class? Or do I need to define a new constructor for Account within InvestmentAccount, which tells it to inherit everything?
为什么编译器不只是从 Account 类中读取 Account 的符号构造函数?或者我是否需要在 InvestmentAccount 中为 Account 定义一个新的构造函数,它告诉它继承所有内容?
Thanks
谢谢
采纳答案by Johannes Weiss
use super(customer)
in InvestmentAccount
s constructor.
super(customer)
在InvestmentAccount
s 构造函数中使用。
Java can not know how to call the onlyconstructor Account
has, because it's not an empty constructor. You can omit super()
only if your base class has an empty constuctor.
Java 不知道如何调用唯一的构造函数Account
,因为它不是一个空的构造函数。super()
仅当您的基类具有空构造函数时,您才可以省略。
Change
改变
public InvestmentAccount(Person customer, int sharePrice)
{
this.customer = customer;
sharePrice = sharePrice;
}
to
到
public InvestmentAccount(Person customer, int sharePrice)
{
super(customer);
sharePrice = sharePrice;
}
that will work.
那可行。
回答by Guillaume
You have to call the constructor of the base class explicitly if the base class doesn't have a default constructor (one with no arguments).
如果基类没有默认构造函数(没有参数的构造函数),则必须显式调用基类的构造函数。
In your case, the constructor should be:
在你的情况下,构造函数应该是:
public InvestmentAccount(Person customer, int sharePrice) {
super(customer);
sharePrice = sharePrice;
}
And don't redefine customer
as an instance variable of the subclass!
并且不要重新定义customer
为子类的实例变量!
回答by Ritesh M Nayak
Call the super() method. If you want to call the Account(Person) constructor, use the statement super(customer); Also this should be the first statment in your InvestmentAccount constructor
调用 super() 方法。如果要调用 Account(Person) 构造函数,请使用语句 super(customer); 这也应该是您的 InvestmentAccount 构造函数中的第一条语句
回答by Bhushan Bhangale
Either define a default constructor in the Account
class:
在Account
类中定义一个默认构造函数:
public Account() {}
Or call super(customer)
in the InvestmentAccount
constructor.
或者super(customer)
在InvestmentAccount
构造函数中调用。
回答by Edison Gustavo Muenz
You have to invoke the superclass constructor, otherwise Java won't know what constructor you are calling to build the superclass on the subclass.
您必须调用超类构造函数,否则 Java 将不知道您正在调用哪个构造函数来在子类上构建超类。
public class InvestmentAccount extends Account {
protected int sharePrice;
protected int numShares;
private Person customer;
public InvestmentAccount(Person customer, int sharePrice) {
super(customer);
this.customer = customer;
sharePrice = sharePrice;
}
}