2015-09-11 92 views
2

我試圖讓我的頭回到VB.Net(已​​經有一段時間)和 似乎無法解決這個簡單的問題。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 

我試圖改變類型myAnimalCow所以我可以訪問它的屬性FriendlyName但我不能讓它在沒有

  1. 創建一個單獨的對象並進行設置。
  2. IAnimal接口聲明FriendlyName .. 我試圖鑄造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 
    

可能是一些愚蠢的,但我在這裏跑出來的選擇。

回答

2

您聲明myAnimal as IAnimal。 IAnimal沒有該屬性FriendlyName

因此,你必須投IAnimalCow

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

OR

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

+0

謝謝,我已經知道這一點,但我需要一個來自myAnimal的對象ICow。 – grmbl

0

如果我想使用新投的對象爲對象本身,
我需要創建一個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 
+0

每次使用對象'myAnimal'時,都必須進行轉換,因爲它被聲明爲IAnimal。你必須告訴編譯器你正在等待的具體類型。 – user2190035

+0

以上不編譯,我得到演員從鷹到ICow是無效的。那麼如何創建一個具有相同基類並實現相同基本接口的對象呢?將myAnimal轉換爲ICow對象類型? – grmbl