0

Consider the following example in 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
Public Class Cycle
    Private num_of_wheels As Integer

    Property Wheels As Integer
        Get
            Return num_of_wheels
        End Get
        Set(ByVal value As Integer)
            num_of_wheels = value
        End Set
    End Property
End Class
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

The above program is showing an error which states that "'getTyp' is not a member of Question.Cycle in Line # 1" here 'Question' is my Project name.

Kindly clarify this concept to me. What needs to be done?

4

1 回答 1

1

尝试:

DirectCast(myCycle, SportsCycle).getTyp

这样做的原因是它Cycle不包含这个属性SportsCycle。由于SportsCycle继承自 Cycle,您可以强制转换为SportsCycle来访问该属性。

于 2013-07-15T12:26:17.350 回答