VB.Net Tutorial/Class Module/MyClass

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

Use MyClass

Class Base
    Public Function Message() As String
        Message = "Hello, World from Base Class"
    End Function
    Public Overridable Function Message2() As String
        Message2 = "Base Class Message 2"
    End Function
    Public Sub ShowMessages()
        Console.WriteLine(MyClass.Message())
        Console.WriteLine(MyClass.Message2())
    End Sub
End Class
Class Derived
    Inherits Base
    Public Shadows Function Message() As String
        Message = "Hi, from Derived Class"
    End Function
    Public Overrides Function Message2() As String
        Message2 = "Derived Class Message 2"
    End Function
End Class
Module Module1
    Sub Main()
        Dim BaseObj As New Base()
        Dim DerivedObj As New Derived()
        Console.WriteLine("Base object Message: " & BaseObj.Message())
        Console.WriteLine("Derived object Message: " & DerivedObj.Message())
        Dim MixedObj As Base = New Derived()
        Console.WriteLine("Mixed message: " & MixedObj.Message())
        Console.WriteLine("Base object Message2: " & BaseObj.Message2())
        Console.WriteLine("Derived object Message2: " & DerivedObj.Message2())
        Console.WriteLine("Mixed message2: " & MixedObj.Message2())
        Console.WriteLine()
        Console.WriteLine("Mixed using MyClass")
        MixedObj.ShowMessages()
    End Sub
End Module
Base object Message: Hello, World from Base Class
Derived object Message: Hi, from Derived Class
Mixed message: Hello, World from Base Class
Base object Message2: Base Class Message 2
Derived object Message2: Derived Class Message 2
Mixed message2: Derived Class Message 2
Mixed using MyClass
Hello, World from Base Class
Base Class Message 2