How to create a subclass in C#?

前端 未结 4 1392
离开以前
离开以前 2020-12-30 02:14

How do I create a subclass in C# for ASP.NET using Visual Studio 2010?

4条回答
  •  粉色の甜心
    2020-12-30 03:07

    This page explains it well:

    public class SavingsAccount : BankAccount
    {
        public double interestRate;
    
        public SavingsAccount(string name, int number, int balance, double rate) : base(name, number)
        {
            accountBalance = balance;
            interestRate = rate;
        }
    
        public double monthlyInterest()
        {
            return interestRate * accountBalance;
        }
    }
    
    static void Main()
    {
        SavingsAccount saveAccount = new SavingsAccount("Fred Wilson", 123456, 432, 0.02F);
    
        Console.WriteLine("Interest this Month = " + saveAccount.monthlyInterest());
    }
    

    If the monthlyInterest method already exists in the BankAccount class (and is declared abstract, virtual, or override) then the SavingsAccount method definition should include override, as explained here. Not using override to redefine such class methods will result in a CS0108 compiler warning, which can be suppressed by using new as confusingly stated here.

提交回复
热议问题