Asp.net 中的另一个 OOP 问题

发布于 2024-07-17 20:13:20 字数 2263 浏览 0 评论 0原文

现在我希望以下内容是可能的,尽管我不完全确定它是这样,所以这里...

下面是我希望可能的设置(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-07-24 20:13:20

我认为您想要做的是定义一个由 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.

狂之美人 2024-07-24 20:13:20

我认为这是因为 GetSearch 是在 A 中实现的,所以它使用 A 中的字段。

通常,如果您不在遗留代码中使用 Shadows,您应该问自己做错了什么。

在这里,这个设计有一种味道。 从 A 继承 B 和从 SearchA 继承 SearchB 这两种情况都是滥用继承。 这有点像从狗那里继承了猫,因为它们“基本上是一样的”。 正如 Jamie Ide 已经说过的:你应该使用抽象基类。

考虑:

  • 使用抽象基类并继承 A 和 B
  • 使用由 SearchA 和 SearchB 实现的接口 ISearch,即
    由基类引用。 这是一种经典的策略模式。
  • 如果您需要对 SearchA 或 SearchB 的类型化引用,请使用泛型。 你
    可能不需要它。
  • 使用工厂将东西组合在一起

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:

  • use a abstract baseclass and inherit A and B
  • Use a interface ISearch implemented by SearchA and SearchB that is
    reference by the base class. It's a classical strategy pattern.
  • Use generics if you need a typed references to SearchA or SearchB. You
    probably don't need it.
  • use factories to put things together
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文