ben*_*wad 10 java polymorphism inheritance
我正在从一个继承自另一个类的类,但我收到一个编译错误,说"找不到符号构造函数Account()".基本上我要做的是创建一个类别的InvestmentAccount,它来自账户 - 账户是为了与取款/存款方法保持平衡,而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...
Run Code Online (Sandbox Code Playgroud)
Person类保存在另一个文件(Person.java)中.现在这里是超类帐户的前几行:
public class Account
{
private Person customer;
protected int balanceInPence;
public Account(Person customer)
{
this.customer = customer;
balanceInPence = 0;
}
// etc...
Run Code Online (Sandbox Code Playgroud)
有没有理由为什么编译器不只是从Account类中读取Account的符号构造函数?或者我是否需要在InvestmentAccount中为Account定义一个新的构造函数,它告诉它继承所有内容?
谢谢
Joh*_*iss 25
super(customer)在InvestmentAccounts构造函数中使用.
Java无法知道如何调用唯一的构造函数Account,因为它不是一个空的构造函数.super()仅当基类具有空构造时,才可以省略.
更改
public InvestmentAccount(Person customer, int sharePrice)
{
this.customer = customer;
sharePrice = sharePrice;
}
Run Code Online (Sandbox Code Playgroud)
至
public InvestmentAccount(Person customer, int sharePrice)
{
super(customer);
sharePrice = sharePrice;
}
Run Code Online (Sandbox Code Playgroud)
那可行.
| 归档时间: |
|
| 查看次数: |
31738 次 |
| 最近记录: |