?????????? ?? ????? ??? ????? C# ???? ????

???? ?????? ???? ??????
???? ID: 307205 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ??? ?? ??? ???? ???? ?????????? ?? ????? ??? ????? C# ???? ?????? ???

?????????? ??? ?? ?????????? ????????-??????? concept ??? ?? ?? ????????? ?? ??????? ???? ????? ?? ???, ?? ?????? ???? ??? ????????? ??????????? ?? ???: ????? ???? ?? ??? ?? ?? ?????? ???? ???

????? ?????? ??? ?????? ?????? ?? ?? ??????? bank ???? ?? ??? ??????? ???? ???? ????????? ?????? ?? ?? derived ???? bank ???? ?? ??? ????? ?????? ?? ???????????? ???? ???? ????????? ?????? Derived ???? ???? ??????? ?? ???? ???? ?? inherit, ????? ??????? ?? ??????? ????, ?? ???? ?? ?? ?????? ?? ??? ?? ????? ??????

??????????

????? ?????? ???????:
  • C# ????????
  • ????????-??????? ?????????

??? ??? ????? ????????? ?????

  1. Visual Studio ??????? ???? ?? ??? ??? C# ????? ????????? ????????? ??? ?????UseInheritance.
  2. ????????? ???????

???????????? ???? ???? ?????

  1. ?????? Explorer ???, ????? ???? ????? ?? ??? (??? ??, ????? ???? ??? ???? ?????????? ???????????????????)? UseInheritance ????????? ?? ????-????? ????, ?? ????? ????add?? ????-????? ????, ?? ???? ???????.

    ???:????? C# .NET 2003, ??? ????? ???? ?? ???add?? ????-????? ????, ?? ???? ???????? ??????.
  2. ??????? ???? ??? ??????????, ??? ?? ???? ??? ?? ???????????? ????-????? ????, ?? ???? ???add.

    ???? ???? ????????? ???? ???? ??? ?????? ????? ??? ???? ?? ??? ????????? ?? ???????????? modifiers ?????? ????? ?? ????? ?? ???? ?????
    public abstract class Account 
    {
       
    
    }

??? ?? ???? ???? ?? ??? ?????

  1. In the Code View window, add two fields to the Account class to represent the account balance and the name of the account holder:
    private string name;        // Only accessible in base class
    protected double balance;   // Accessible in base class and derived class
  2. Modify the constructor as follows to initialize these fields:
    public Account(string nm, double bal)
    {
       this.name = nm;
       this.balance = bal;
    
    }
  3. Add the following methods to the class. Thevirtualkeyword means these methods can be overridden in derived classes:
    public virtual void Credit(double amount)
    {
       this.balance += amount;
    }
    
    public virtual void Debit(double amount)
    {
       this.balance -= amount;
    }
    
    public virtual void Display()
    {
       Console.WriteLine("Name={0}, balance={1}", this.name, this.balance);
    
    }
  4. Add the following method to the class. Because this method is not marked as virtual, it cannot be overridden in derived classes. This method provides the capability to change the name of the account holder.
    public void ChangeName(string newName)
    {
       this.name = newName;
    
    }
  5. Add the following method to the class. Theabstract?????? ?? ???? ?? ?? ???? derived ???? ??? overridden ???? ?????? ??:
    public abstract double CalculateBankCharge();

??? Derived ???? ?????

  1. ??????????? ?????, UseInheritance ????????? ?? ????-????? ????? ????? ????ShortCut????, ' ?????add?? ????-????? ????, ?? ???? ???????.
  2. ??????? ???? ??? ?????????? ????? ???, ??????SavingsAccount?? ????-????? ????, ?? ???? ???add.
  3. SavingsAccount ???? ?? ??? ??? ??????
  4. ????? SavingsAccount ???? ??????? ???????????, ????? SavingsAccount inherits ?? ????:
    public class SavingsAccount : Account
    {
       
    
    }

??? ?? Derived ???? ?? ??? ?????

  1. SavingsAccount ???? ?? ??? ??? ????? ?????:
    private double minBalance;   // If the balance drops below minBalance, 
                                 // the bank will charge a fee on the account
  2. ????? ???? ???? ??? ?? ?? ???? ??? ??????? ???? ?? ??? SavingsAccount constructor ??????? ????:
    public SavingsAccount(string nm, double bal, double min) 
       : base(nm, bal)           // Call base-class constructor first
    {
       minBalance = min;         // Then initialize fields in this class
    
    }
  3. SavingsAccount ???? ?? ??? ????? ??????? ??? ??????? ?? ??????? ??????? ??????? ??????? ???? ???? ?? ??????? ?? ?? ??:
    public override void Debit(double amount)
    {
       if (amount <= balance)    // Use balance, inherited from base class
          base.Debit(amount);    // Call Debit, inherited from base class 
    }
    public override void Display()
    {
       base.Display();          // Call Display, inherited from base class
       Console.WriteLine("$5 charge if balance goes below ${0}", minBalance);
    
    }
  4. ?? ???? ???? ?? ??? ??? ??????? ?? ??????? ???? ????? SavingsAccount ???? ?? ??? ????? ???? ??????:
    public override double CalculateBankCharge()
    {
       if (balance < minBalance)
          return 5.00;
       else
          return 0.00;
    
    }

??? ?????? ??????

  1. ??? ????? ????? ??? Class1.cs ?? ??? ??? ????????? ?????
  2. ?????????????? ??????, ?? SavingsAccount ???????? ????? ?? ?? ??? ??? ????????? ????:
    SavingsAccount sa = new SavingsAccount("Freda Smith", 100.00, 25);
    sa.Display();
  3. SavingsAccount ?? ???? ??? ????????? ??????? ?? ??? ???? ?? ??? ????? ??? ??????:
    sa.Credit(100);
    sa.Debit(180);
    sa.ChangeName("Freda Jones");
    sa.Display();
    Console.WriteLine("Bank charge: ${0}",  sa.CalculateBankCharge());
  4. ????????? ??????
  5. ??? breakpoint ?? ??????? ?? ??? ?????????????, ?? ????? ??? ????????? ??????? ????? ???????? ??? ??? ???, ?? ????????? ?? ????? ??? ?? ???? ?????? ??? ????? ????????? ????? ?? ????? ??????? ????????? ???? ??:
    Name=Freda Smith, balance=100
    $5 charge if balance goes below $25
    Name=Freda Jones, balance=20
    $5 charge if balance goes below $25
    Bank charge: $5

???

???? ID: 307205 - ????? ???????: 29 ??????? 2010 - ??????: 4.0
???? ???? ???? ??:
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2003 Standard Edition
??????: 
kbsweptvs2008 kbhowtomaster kbmt KB307205 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:307205

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com