为什么我不能在c#中继承类?

时间:2019-03-19 18:57:21

标签: c# inheritance

我试图创建一个基本的银行系统来练习使用类,在创建父类“ 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";
    }
}

如果有人能帮助我,请先感谢!

1 个答案:

答案 0 :(得分:0)

代码应与此类似(按基础更改帐户)

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";
    }
}