VB.Net Tutorial/Class Module/Shadows — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:55, 26 мая 2010

Hiding a member

Public Class ClassX
    Public s As String = "x"
End Class
Public Class ClassY
    Inherits ClassX
    Public Shadows s As String = "y"
    Public Function getDerivedS() As String
        Return s
    End Function
    Public Function getBaseS() As String
        Return MyBase.s
    End Function
End Class

Shadows Override

Public Class ClassA
    Public Function greet1() As String
        Return "Hello from classA, Greet1 (no modifiers)"
    End Function
    
    Public Overridable Function greet2() As String
        Return "Hello from classA, Greet2 (overridable)"
    End Function
End Class
Public Class ClassB
    Inherits ClassA
    
    Public Shadows Function greet1() As String
        Return "Hello from ClassB, Greet1 (shadows)"
    End Function
    
    Public Overrides Function greet2() As String
        Return "Hello from ClassB, Greet2 (overrides)"
    End Function
End Class

Test Shadows

public class Test
   public Shared Sub Main
        
        Dim mgr As New Manager
        mgr.FirstName = "Manager Last Name"
        mgr.EmployeeId = 1
        
        Dim emp As Employee = CType(mgr, Employee)
        emp.FirstName = "Employee Last Name"
        emp.EmployeeId = 2
        
        Dim per As Person = CType(mgr, Person)
        per.FirstName = "Person Last Name"
        per.EmployeeId = "A"
        
        Console.WriteLine(mgr.EmployeeId & ": " & mgr.FirstName)
        Console.WriteLine(emp.EmployeeId & ": " & emp.FirstName)
        Console.WriteLine(per.EmployeeId & ": " & per.FirstName)
   End Sub
End class

Public Class Person
    Public FirstName As String
    Public EmployeeId As String
End Class
Public Class Employee
    Inherits Person
    Public Shadows EmployeeId As Long
End Class
Public Class Manager
    Inherits Employee
    Public Shadows FirstName As String
End Class
2: Manager Last Name
2: Person Last Name
A: Person Last Name

Use "Private Shadows" to hide member

Public Class Base
    Public Function Greet1() As String
        Return "Greetings from Base"
    End Function
End Class
Public Class Derived
    Inherits Base
    Private Shadows Function Greet1() As String
        Return "Greetings from Base"
    End Function
End Class
Public Class Morederived
    Inherits Derived
End Class