2014-01-15 311 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