2014-01-15 46 views
0

我在這裏有一個奇怪的問題,我想答案是否定的,但是......有什麼辦法繼承一個類的prooperties 沒有繼承它,只是由組成?繼承屬性沒有繼承

什麼我現在是這樣的:

Public Class Mixer 
    Inherits SomeOtherClass 

    Private _motor As Motor 

    Public Property Active() As Boolean 
     Get 
      Return _motor.Active 
     End Get 
     Set(ByVal value As Boolean) 
      _motor.Active = value 
     End Set 
    End Property 
    Public Property Frecuency() As Boolean 
     Get 
      Return _motor.Frecuency 
     End Get 
     Set(ByVal value As Boolean) 
      _motor.Frecuency = value 
     End Set 
    End Property 

    'More properties and functions from Mixer class, not from Motor 
    ' 
    ' 
End Class 

所以我需要的類混音器顯示公開所有它的汽車性能,但我不希望繼承電機,因爲我它已經從SomeOtherClass繼承。有沒有更快,更乾淨,更簡單的方法來做到這一點?

謝謝!

編輯: 只是爲了澄清:我知道我可以用一個接口,但由於電機的實現是所有類一樣,我想直接繼承其性能,而無需在其再次實施這些每個類有一個電機...但沒有繼承電機。

+1

你看過接口嗎? http://msdn.microsoft.com/en-us/library/28e2e18x.aspx。 – User999999

+0

是的,但是實現一個接口會使我編寫所有接口的屬性實現,而這正是我想要避免的...... –

+0

如果您只是將'Motor'和「混音器」實現一個通用接口,例如'IMotor'。 –

回答

0

我相信你可以在界面中使用屬性,然後實現該界面。

看一看這個question

0

你總是可以讓你的私人_motor的公共屬性,那麼你最好能去的汽車性能是間接的。我知道這不是你要求的。

0

最廣泛接受的解決方案(如果不是唯一的解決方案)是提取一個通用接口,該接口在包裝Motor實例的每個類中實現。

Public Interface IMotor 

    Property Active As Boolean 

    Property Frequency As Boolean 

End Interface 


Public Class Motor 
    Implements IMotor 

    Public Property Active As Boolean Implements IMotor.Active 

    Public Property Frequency As Boolean Implements IMotor.Frequency 

End Class 


Public Class Mixer 
    Inherits SomeOtherClass 
    Implements IMotor 

    Private _motor As Motor 

    Public Property Active() As Boolean Implements IMotor.Active 
     Get 
      Return _motor.Active 
     End Get 
     Set(ByVal value As Boolean) 
      _motor.Active = value 
     End Set 
    End Property 

    Public Property Frequency() As Boolean Implements IMotor.Frequency 
     Get 
      Return _motor.Frequency 
     End Get 
     Set(ByVal value As Boolean) 
      _motor.Frequency = value 
     End Set 
    End Property 

End Class