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

 

转载于:https://www.cnblogs.com/sxjljj/p/11442041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值