???? ID: 307222 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

Visual Basic 2005 ?? Visual Basic .NET ??? ?????????? ?? ????? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
?? Microsoft Visual C# .NET ??????? ?? ??? ?? ???? ??, ?????307205  (http://support.microsoft.com/kb/307205/ ) .

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

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

?? ???? ??? Microsoft Visual Basic 2005 ?? Microsoft Visual Basic .NET ??? ?????????? ?? ????? ???? ???? ??? ?????? ?? ??? ????? ?? ??????? ??? ??? ??????? bank ???? ?? ??? ?? ???? ???? ????????? ?????? ?? ?? derived ???? bank ???? ?? ??? ????? ?????? ?? ???????????? ???? ???? ????????? ?????? Derived ???? ???? ??????? ?? ???? ???? ?? inherit, ????? ??????? ?? ??????? ????, ?? ?? ?????? ?? ???? ?? ??? ?? ????? ??????

??????????

????? ?????? ???????:
  • Visual Basic 2005 ?? Visual Basic .NET ????????
  • ????????-??????? ?????????

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

  1. Microsoft Visual Studio 2005 ?? Microsoft Visual Studio .NET ?? ??????? ????, ?? ??? ??? Visual Basic ????? ????????? ????????? ??? ?????UseInheritance?? ???? ??????? ???????? ???
  2. ????? ????????????????? ??,????? ??????.
  3. ??????? ???? ??? ?????????? ????? ???, ??????Account.VB????????????? ??? ????? ????,?????.

    ???:Visual Studio 2005, ??? ????? ????add?? ?????????.
  4. Visual Studio .NET displays the file Account.vb.
  5. ????????? ???????

Create an abstract base class

  1. ?????MustInheritmodifier to the Account class, to make Account an abstract class:
    Public MustInherit Class Account
    
    End Class
    					

Write code for the base class

  1. Add two fields to the Account class:
    Private Name As String       ' Only accessible in base class
    Protected Balance As Double  ' Accessible in base class and derived class
  2. Add a constructor to initialize these fields:
    Public Sub New(ByVal Nm As String, ByVal Bal As Double)
       Name = Nm
       Balance = Bal
    End Sub 
  3. Add the following methods to the class. TheOverridablekeyword means these methods can be overridden in derived classes:
    Public Overridable Sub Credit(ByVal Amount As Double)
       Balance += Amount
    End Sub
    
    Public Overridable Sub Debit(ByVal Amount As Double)
       Balance -= Amount
    End Sub
    
    Public Overridable Sub Display()
       Console.WriteLine("Name=" & Name & ", " & "Balance=" & Balance)
    End Sub
  4. Add the following method to the class. Because this method is not marked asOverridable, it cannot be overridden in derived classes. This method provides the capability to change the name of the account holder.
    Public Sub ChangeName(ByVal newName As String)
       Name = newName
    End Sub
  5. Add the following method to the class. TheMustOverridekeyword means this method must be overridden in derived classes:
    Public MustOverride Function CalculateBankCharge() As Double

Create a derived class

  1. ??????????????? ??,????? ??????.
  2. ??????? ???? ??? ?????????? ????? ???, ??????SavingsAccount.vb????????????? ??? ????? ????,?????.
  3. Visual Studio .NET displays the file SavingsAccount.vb.
  4. Change the SavingsAccount class definition as follows, so that SavingsAccount inherits from Account (note that theInheritskeyword must appear on a new line):
    Public Class SavingsAccount 
       Inherits Account
    
    End Class

Write code for the derived class

  1. Add a field to the SavingsAccount class:
    Private MinBalance As Double  ' If the balance drops below MinBalance, 
                                  ' the bank will charge a fee on the account
  2. ????? ???? ???? ??? ?? ?? ???? ??? ??????? ???? ?? ??? ???????????, ?? constructor ??????:
    Public Sub New(ByVal Nm  As String, _
                   ByVal Bal As Double, _
                   ByVal Min As Double)
       MyBase.New(Nm, Bal)        ' Call base-class constructor first
       MinBalance = Min           ' Then initialize fields in this class
    End Sub
  3. SavingsAccount ???? ?? ??? ????? ??????? ??? ??????? ?? ??????? ?? ???????Overridable???? ???? ?? ??????? ?? ?? ????:
    Public Overrides Sub Debit(Amount As Double)
       If Amount <= Balance Then  ' Use balance, inherited from base class
          MyBase.Debit(Amount)    ' Call Debit, inherited from base class
       End If
    End Sub
    
    Public Overrides Sub Display()
       MyBase.Display()           ' Call Display, inherited from base class
       Console.WriteLine("$5 charge if balance goes below $" & MinBalance)
    End Sub
  4. ??? ??????? ???? ????MustOverride???? ???? ?? ???????? SavingsAccount ???? ?? ??? ????? ???? ??????:
    Public Overrides Function CalculateBankCharge() As Double
       If Balance < MinBalance Then
          Return 5.0
       Else
          Return 0.0
       End If
    End Function

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

  1. ??? ????? ????? ??? Module1.vb ?? ??? ??? ????????? ?????
  2. ?????????????? ??????, ?? SavingsAccount ???????? ?? ?? ??? ??? ?????:
    Dim sa As SavingsAccount = 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: $" & sa.CalculateBankCharge())
    					
  4. ????????? ??????
  5. ?????? ???????? ??,??????? ?? ???? ??????? ????????????? ?????? ????????? ????? ?? ????? ??????? ????????? ???? ??:
    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
    						
  6. ????????? ?? ???: ?????, ????? ?? ??? ????? ?? ????? ????? ??? breakpoint ?? ??????? ?? ??? ????????????? ?? ?? ??? ????????????????? ???????? ??.. ???????? ??? ??? ??? ?? ????????? ?? ????? ??? ?? ???? ?????? ??? ???? ????

???? ???? ???? ??:
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbvs2005applies kbvs2005swept kbhowtomaster kbmt KB307222 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:307222  (http://support.microsoft.com/kb/307222/en-us/ )