【发布时间】:2019-08-10 09:47:07
【问题描述】:
我正在尝试创建一个基本的银行系统来练习使用类,在创建父类“Account”之后,我尝试创建一个“储蓄”类,它将作为子类并继承属性和方法,但是,无论我查找什么,都不会告诉我该怎么做。我收到诸如“必须声明一个主体,因为它没有标记为抽象、外部或部分”等错误。我真的不知道该怎么做才能让它工作,所以我希望这里有人可以提供帮助,这是我的代码:
public class Account
{
protected string name;
protected string birthdate;
protected string address;
protected double balance;
protected string status;
protected string type;
public Account(string customerName, string customerBirthdate, string customerAddress, int customerBalance)
{
name = customerName;
birthdate = customerBirthdate;
address = customerAddress;
balance = customerBalance;
status = "Ok";
type = "Basic";
}
public void customerDetails()
{
Console.WriteLine("Name: {0}, Birthdate: {1}, Address: {2}", name, birthdate, address);
}
public void accountDetails()
{
Console.WriteLine("Balance: £{0}, Account Status: {1}, Account Type: {2}", Math.Round(balance, 2), status, type);
}
private void updateStatus()
{
if (balance < 0)
{
status = "Overdrawn";
}
else if (balance > 0 )
{
status = "Ok";
}
}
public void deposit(int amount)
{
balance += amount;
updateStatus();
}
public void withdraw(int amount)
{
balance -= amount;
updateStatus();
}
}
public class Savings : Account
{
public Savings(string customerName, string customerBirthdate, string customerAddress, int customerBalance) : Account(customerName, customerBirthdate, customerAddress, customerBalance)
{
name = customerName;
birthdate = customerBirthdate;
address = customerAddress;
balance = customerBalance;
status = "Ok";
type = "Basic";
}
}
如果有人可以帮助我,请提前感谢!
【问题讨论】:
-
当要调用基类的构造函数时,应使用
base关键字,而不是基类本身的名称。 -
@JonathonChase 是对的,只需在 Savings 类中将 Account 类名称更改为 Base。
public Savings(string customerName, string customerBirthdate, string customerAddress, int customerBalance) : base(customerName, customerBirthdate, customerAddress, customerBalance) -
@JonathonChase 非常感谢大家,真不敢相信我错过了!
标签: c# inheritance