vb.net中运行时多态性的概念

vb.net中运行时多态性的概念,vb.net,Vb.net,考虑VB.NET中的以下示例 Module Module1 Sub Main() Dim myCycle As Cycle 'Here I am making a Superclass reference to hold a subclass object myCycle = New SportsCycle() Console.WriteLine("----Cycle Details--------")

考虑VB.NET中的以下示例

Module Module1
    Sub Main()
        Dim myCycle As Cycle

        'Here I am making a Superclass reference to hold a subclass object
        myCycle = New SportsCycle()
        Console.WriteLine("----Cycle Details--------")

        'Using this Object I am accessing the property Wheels of the Superclass Cycle
        Console.WriteLine("Number Of Wheels: " & myCycle.Wheels)

        'Using this Object I am accessing the property getTyp of the Subclass Cycle
        Console.WriteLine("Type Of Cycle: " & myCycle.getTyp) 'Line #1(This Line is showing error)

        Console.WriteLine("--------------------------")
        Console.ReadKey()
    End Sub
End Module
上面的程序显示了一个错误,指出“'getTyp'不是 问题。在第1行循环“这里‘问题’是我的项目名称

请向我澄清这个概念。需要做什么?

试试:

DirectCast(myCycle, SportsCycle).getTyp

原因是,
Cycle
不包含此属性,而as
SportsCycle
包含此属性。由于
SportsCycle
继承自Cycle,您可以强制转换到
SportsCycle
以访问该属性。

请正确缩进和格式化代码,并删除所有不必要的空行和注释,因为它们不会添加信息,从而大大降低可读性。您需要将其声明为子类,或者根据Ric的答案将其转换为子类。帮自己一个忙,将
选项设置为Strict On
,让编译器捕获此类错误,而不是等待它在运行时爆发。仅供参考,“type”是VB中的一个关键字。用别的东西。
Public Class SportsCycle
    Inherits Cycle

    Private type As String

    Sub New()
        type = "RAZORBIKE"
        Wheels = 2
    End Sub

    ReadOnly Property getTyp As String
        Get
            Return type
        End Get
    End Property
End Class
DirectCast(myCycle, SportsCycle).getTyp