英文:
VB.Net How to pass pass different objects into sub
问题
如何设置这个子程序以便你可以传递不同的对象进去?
Protected Shared Sub exampleSub(Optional ByRef partDescription as DoorDescription = Nothing)
For Each feat In partDescription.MirrorFeatures
...
Next
End Sub
Public Class DoorDescription
Public Property MirrorFeatures As New List(Of Feature)
End Class
Public Class WindowDescription
Public Property MirrorFeatures As New List(Of Feature)
End Class
exampleSub(DoorDescription)
exampleSub(WindowDescription)
希望设置这个子程序以便它能够适用于DoorDescription和WindowDescription。
英文:
How do you setup this sub so that you can pass different objects into it?
Protected Shared Sub exampleSub(Optional ByRef partDescription as DoorDescription = Nothing)
For Each feat In partDescription.MirrorFeatures
...
Next
End Sub
Public Class DoorDescription
Public Property MirrorFeatures As New List(Of Feature)
End Class
Public Class WindowDescription
Public Property MirrorFeatures As New List(Of Feature)
End Class
exampleSub(DoorDescription)
exampleSub(WindowDescription)
Looking to setup this sub so that it will work with DoorDescription and WindowDescription.
答案1
得分: 3
将代码翻译为中文:
创建一个抽象类,从中继承WindowDescription和DoorDescription
```vb.net
Public MustInherit Class EntrywayDescription
Public Property MirrorFeatures As List(Of Feature)
End Class
同样,一个接口也可以工作
Public Interface IEntrywayDescription
Property MirrorFeatures As IEnumerable(Of Feature)
End Interface
然后你的类可以继承(或在接口的情况下实现)基类或接口
' 抽象类
Public Class DoorDescription
Inherits EntrywayDescription
End Class
' 接口
Public Class WindowDescription
Implements IEntrywayDescription
End Class
你可以这样使用它。如果两个类之间有更多的共同功能,你可以将它添加到基类中,并在引用是基类类型时访问它
Public Sub 示例(partDescription As EntrywayDescription)
For Each feat In partDescription.MirrorFeatures
Next
End Sub
通常,如果两个类之间有共享的具体功能,你可能会希望使用基类继承方法。例如,
Public MustInherit Class EntrywayDescription
Public Property MirrorFeatures As List(Of Feature)
Public ReadOnly Property GetFeaturesString As String
Get
Return String.Join(", ", MirrorFeatures.Select(Function(f) f.ToString()))
End Get
End Property
End Class
否则,使用接口,但然后你需要在每个实现它的类中单独实现接口成员,例如
Public Class DoorDescription
Implements IEntrywayDescription
Private _mirrorFeatures As List(Of Feature)
Public Property MirrorFeatures As IEnumerable(Of Feature) Implements IEntrywayDescription.MirrorFeatures
Get
Return _mirrorFeatures
End Get
Set(value As IEnumerable(Of Feature))
_mirrorFeatures = value.ToList()
End Set
End Property
End Class
希望这对你有所帮助。
英文:
Make an abstract class from which both the WindowDescription and DoorDescription inherit
Public MustInherit Class EntrywayDescription
Public Property MirrorFeatures As List(Of Feature)
End Class
Similarly, an Interface would work
Public Interface IEntrywayDescription
Property MirrorFeatures As IEnumerable(Of Feature)
End Interface
Then your classes would Inherit (or Implement in the case of the Interface) the base class or Interface
' abstract class
Public Class DoorDescription
Inherits EntrywayDescription
End Class
' interface
Public Class WindowDescription
Implements IEntrywayDescription
End Class
You can use it like this. If you have more common functionality between both classes, you can add it to the base class and access when the reference is of the base class type
Public Sub example(partDescription As EntrywayDescription)
For Each feat In partDescription.MirrorFeatures
Next
End Sub
Typically you may want to use the base class inheritance method if you have shared concrete functionality between the two classes. For example,
Public MustInherit Class EntrywayDescription
Public Property MirrorFeatures As List(Of Feature)
Public ReadOnly Property GetFeaturesString As String
Get
Return String.Join(", ", MirrorFeatures.Select(Function(f) f.ToString()))
End Get
End Property
End Class
Use the Interface otherwise, but you will then need to implement the interface members individually in each class which implements it, for example
Public Class DoorDescription
Implements IEntrywayDescription
Private _mirrorFeatures As List(Of Feature)
Public Property MirrorFeatures As IEnumerable(Of Feature) Implements IEntrywayDescription.MirrorFeatures
Get
Return _mirrorFeatures
End Get
Set(value As IEnumerable(Of Feature))
_mirrorFeatures = value.ToList()
End Set
End Property
End Class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论