为啥我不能在 C# 中继承一个类? [复制]
Posted
技术标签:
【中文标题】为啥我不能在 C# 中继承一个类? [复制]【英文标题】:Why can't I inherit a class in c#? [duplicate]为什么我不能在 C# 中继承一个类? [复制] 【发布时间】: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
关键字,而不是基类本身的名称。
docs.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance 和 tutorialspoint.com/csharp/csharp_inheritance.htm
@JonathonChase 是对的,只需在 Savings 类中将 Account 类名称更改为 Base。 public Savings(string customerName, string customerBirthdate, string customerAddress, int customerBalance) : base(customerName, customerBirthdate, customerAddress, customerBalance)
@JonathonChase 非常感谢大家,真不敢相信我错过了!
【参考方案1】:
代码应该是这样的(按基地更改帐户)
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)
this.name = customerName;
this.birthdate = customerBirthdate;
this.address = customerAddress;
this.balance = customerBalance;
this.status = "Ok";
this.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) : base (customerName, customerBirthdate, customerAddress, customerBalance)
base.name = customerName;
base.birthdate = customerBirthdate;
base.address = customerAddress;
base.balance = customerBalance;
base.status = "Ok";
base.type = "Basic";
【讨论】:
以上是关于为啥我不能在 C# 中继承一个类? [复制]的主要内容,如果未能解决你的问题,请参考以下文章