vb.net 实现多态

Posted sxjljj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 实现多态相关的知识,希望对你有一定的参考价值。

1. 新建一个module,设置public 其他类才可以调用

Public Module Module2
    Public Interface MyInterface
        Property stuName As String
        Function GetScore(ByVal x As Single) As Single
    End Interface

    Public Class StuInfo
        Implements MyInterface
        Private studentScore As Single
        Private studentName As String
        Public Property Score() As Single
            Get
                Return studentScore
            End Get
            Set(ByVal value As Single)
                studentScore = value
            End Set
        End Property
        Public Function GetScore(ByVal x As Single) As Single Implements MyInterface.GetScore
            Return x * 0.8
        End Function

        Public Property StuName() As String Implements MyInterface.stuName
            Get
                Return studentName
            End Get
            Set(ByVal value As String)
                studentName = value
            End Set
        End Property
    End Class


    Public Class StuMessage
        Implements MyInterface
        Private studentName As String
        Private stuScore As Single
        Public Property Score() As Single
            Get
                Return stuScore
            End Get
            Set(ByVal value As Single)
                stuScore = value
            End Set
        End Property
        Public Function GetScore(ByVal x As Single) As Single Implements MyInterface.GetScore
            Return x * 0.8
        End Function

        Public Property StuName() As String Implements MyInterface.stuName
            Get
                Return studentName
            End Get
            Set(ByVal value As String)
                studentName = value
            End Set
        End Property

    End Class

End Module

2. 测试页面:

Public Class TestClassForm
    Public Sub ShowScore(ByVal obj As MyInterface, ByVal s As String, ByVal score As Single)
        MsgBox(s & "成绩为:" & obj.GetScore(score) & "")
    End Sub
    Private Sub TestClassForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim obj1 As New StuInfo()
        Dim obj2 As New StuMessage()
        ShowScore(obj1, "期中", 100)
        ShowScore(obj2, "期末", 100)
    End Sub
End Class

 

以上是关于vb.net 实现多态的主要内容,如果未能解决你的问题,请参考以下文章

vb.net 导出PDF

VB.net中如何嵌套EXCEL?

使用 C#/VB.NET 实现 SQL Server Profiler

VB.Net和C#.Net有啥差异?优缺点是啥?

C# 等效于 VB.NET 接口实现

vb.net的数据库连接