Asp.net 中的另一个 OOP 问题
现在我希望以下内容是可能的,尽管我不完全确定它是这样,所以这里...
下面是我希望可能的设置(在 VB.net 中,请随意用 C# 回答,我应该能够解决它):
Public Class A
Private _name As String
Private _s As SearchA
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
Public Property S() As SearchA
Get
Return _s
End Get
Set(ByVal Value As SearchA)
_s = Value
End Set
End Property
Public Sub New(ByVal name As String)
_name = name
_s = New SearchA()
End Sub
Public Function GetSearch() As String
Return _s.Search
End Sub
End Class
并且
Public Class SearchA
Private _search As String
Public Property Search () As String
Get
Return _search
End Get
Set(ByVal Value As String
_search = Value
End Set
End Property
Public Sub New()
_search = "Search using Base"
End Sub
End Class
最后
Public Class B
Inherits A
Private Shadows _s As SearchB
Public Shadows Property S() As SearchB
Get
Return _s
End Get
Set(ByVal Value As SearchB)
_s = Value
End Set
End Property
Public Sub New(ByVal name As String)
Mybase.New(name)
_s = New SearchB()
End Sub
End Class
,
Public Class SearchB
Inherits SearchA
Private _superSearch As String
Public Property SuperSearch () As String
Get
Return _superSearch
End Get
Set(ByVal Value As String
_superSearch = Value
End Set
End Property
Public Sub New()
Mybase.New()
_search = "Search using New"
_superSearch = "With more options..."
End Sub
End Class
这是用法:
Dim oB As New B("hello")
Response.Write(oB.GetSearch())
我认为阴影可能会工作并打印“使用新的搜索”,但事实并非如此,有什么想法吗? 我无法覆盖该属性 基类属性的不同返回类型。 我想在基类中定义一组我没有的核心函数 在每个子类中覆盖。 或者这没有多大意义?
感谢您的帮助!
史蒂夫
now i'm hoping the following is possible although I'm not entirely certain it is so here goes...
Below is the setup of what I'm hoping is possible (in VB.net, feel free to answer in C# and I should be able to work it out):
Public Class A
Private _name As String
Private _s As SearchA
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
Public Property S() As SearchA
Get
Return _s
End Get
Set(ByVal Value As SearchA)
_s = Value
End Set
End Property
Public Sub New(ByVal name As String)
_name = name
_s = New SearchA()
End Sub
Public Function GetSearch() As String
Return _s.Search
End Sub
End Class
and
Public Class SearchA
Private _search As String
Public Property Search () As String
Get
Return _search
End Get
Set(ByVal Value As String
_search = Value
End Set
End Property
Public Sub New()
_search = "Search using Base"
End Sub
End Class
and
Public Class B
Inherits A
Private Shadows _s As SearchB
Public Shadows Property S() As SearchB
Get
Return _s
End Get
Set(ByVal Value As SearchB)
_s = Value
End Set
End Property
Public Sub New(ByVal name As String)
Mybase.New(name)
_s = New SearchB()
End Sub
End Class
and finally
Public Class SearchB
Inherits SearchA
Private _superSearch As String
Public Property SuperSearch () As String
Get
Return _superSearch
End Get
Set(ByVal Value As String
_superSearch = Value
End Set
End Property
Public Sub New()
Mybase.New()
_search = "Search using New"
_superSearch = "With more options..."
End Sub
End Class
and here's the usage:
Dim oB As New B("hello")
Response.Write(oB.GetSearch())
I thought that shadows might work and print "Search using New" but it doesn't, any ideas? I can't override as the property has
a different return type to the base class property. I want to define within a base class a core set of functions that I don't have
to override within each child class. Or does this not make much sense?
Thanks for your help!
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您想要做的是定义一个由 SearchA 和 SearchB 继承的抽象 Search 类。 将 _s 设为受保护的搜索类型,将其公开为公共属性,并在 A 类中将 SearchA 分配给它,将 B 类中的 SearchB 分配给它。然后 GetSearch() 将从 A 返回 SearchA,从 B 返回 SearchB,尽管它们都将被键入作为搜索。
I think what you want to do is define an abstract Search class that is inherited by SearchA and SearchB. Make _s a protected Search type, expose it as a public property, and assign SearchA to it in class A and SearchB to it in class B. Then GetSearch() will return SearchA from A and SearchB from B, although they will both be typed as Search.
我认为这是因为 GetSearch 是在 A 中实现的,所以它使用 A 中的字段。
通常,如果您不在遗留代码中使用 Shadows,您应该问自己做错了什么。
在这里,这个设计有一种味道。 从 A 继承 B 和从 SearchA 继承 SearchB 这两种情况都是滥用继承。 这有点像从狗那里继承了猫,因为它们“基本上是一样的”。 正如 Jamie Ide 已经说过的:你应该使用抽象基类。
考虑:
由基类引用。 这是一种经典的策略模式。
可能不需要它。
I think it is because GetSearch is implemented in A, so it uses the fields from A.
Generally, if you use Shadows not in legacy code, you should ask yourself what you are doing wrong.
As it is here, this design has a smell. Both cases of inheritance B from A and SearchB from SearchA are kind of misusing inheritance. It's kind of inheriting a cat from a dog because it's "mostly the same". As Jamie Ide already said: you should use abstract base classes.
Consider to:
reference by the base class. It's a classical strategy pattern.
probably don't need it.