繁体   English   中英

VB.Net 类型转换

[英]VB.Net Type conversion

我试图让我的头回到 VB.Net(已​​经有一段时间了)并且似乎无法解决这个简单的问题。

这是我的抽象基类及其接口:

Imports VBRefresh.Animal

Public Interface IAnimal
    ReadOnly Property AnimalType() As TypeOfAnimal
    Property IsAlive As Boolean
End Interface

Public MustInherit Class Animal
    Implements IAnimal

    Public Enum TypeOfAnimal
        Insect
        Mammal
        Fish
        Bird
        Reptile
    End Enum

    Private _animalType As TypeOfAnimal
    Public ReadOnly Property AnimalType As TypeOfAnimal Implements IAnimal.AnimalType
        Get
            Return _animalType
        End Get
    End Property

    Private _isAlive As Boolean
    Public Property IsAlive() As Boolean Implements IAnimal.IsAlive
        Get
            Return _isAlive
        End Get
        Set(ByVal value As Boolean)
            _isAlive = value
        End Set
    End Property

    Public Sub New(animalType As TypeOfAnimal)
        _animalType = animalType
    End Sub

End Class

这里有两个派生类(其中Cow扩展了IAnimal ):

Public Interface ICow
    Inherits IAnimal

    Property FriendlyName As String
End Interface

Public Class Cow
    Inherits Animal
    Implements ICow

    Public Sub New()
        MyBase.New(TypeOfAnimal.Mammal)
    End Sub

    Private _friendlyName As String
    Public Property FriendlyName As String Implements ICow.FriendlyName
        Get
            Return _friendlyName
        End Get
        Set(value As String)
            _friendlyName = value
        End Set
    End Property

End Class

Public Class Eagle
    Inherits Animal

    Public Sub New()
        MyBase.New(TypeOfAnimal.Bird)
    End Sub

End Class

我正在尝试将myAnimal类型更改为Cow以便我可以访问它的属性FriendlyName但没有它我无法让它工作

  1. 创建一个单独的对象并设置它。
  2. IAnimal接口中声明FriendlyName .. 我尝试将ICow myAnimalICow但这并没有让我访问FriendlyName属性?

     Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load Dim myAnimal As IAnimal = New Eagle myAnimal.IsAlive = True myAnimal = New Cow myAnimal.FriendlyName = "Bella" 'this line doesn't work Dim myCow As ICow = New Cow myCow.FriendlyName = "Bella" MessageBox.Show(myAnimal.AnimalType.ToString) End Sub

可能有些愚蠢,但我在这里没有选择。

myAnimal as IAnimal声明myAnimal as IAnimal IAnimal 没有 Property FriendlyName

因此,您必须将IAnimalCow

DirectCast(myAnimal, Cow).FriendlyName = "Bella"

或者

DirectCast(myAnimal, ICow).FriendlyName = "Bella"

如果我想将新投射的对象用作对象本身,
我需要创建一个 ICow 类型的对象,我将铸造对象分配给它:

Public Class Main
    Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim myAnimal As IAnimal = New Eagle
        myAnimal.IsAlive = True

        myAnimal = New Cow
        myAnimal.FriendlyName = "Bella" 'this line doesn't work

        myAnimal = DirectCast(myAnimal, ICow)
        myAnimal.FirendlyName = "Bella" 'this is still unavailble?

        'guess I have to solve it this way then:
        Dim myCow As ICow = DirectCast(myAnimal, ICow)
        myCow.FriendlyName = "Bella"

        MessageBox.Show(myAnimal.AnimalType.ToString)
    End Sub
End Class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM