Asp.net中的另一个OOP问题

时间:2021-06-15 08:10:03

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):

下面是我希望可能的设置(在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

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

2 个解决方案

#1


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.

我想你想要做的是定义一个由SearchA和SearchB继承的抽象搜索类。使_s成为受保护的搜索类型,将其作为公共属性公开,并在类A中将SearchA分配给它,然后在类B中将SearchA分配给它。然后,GetSearch()将从A返回SearchA,从B返回SearchB,尽管它们都将被输入作为搜索。

#2


I think it is because GetSearch is implemented in A, so it uses the fields from A.

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

Generally, if you use Shadows not in legacy code, you should ask yourself what you are doing wrong.

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

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.

就像在这里一样,这种设计有一种气味。来自A的继承B和来自SearchA的SearchB的两种情况都是滥用继承。它是从狗身上继承一只猫,因为它“大部分都是一样的”。正如Jamie Ide已经说过:你应该使用抽象基类。

Consider to:

  • use a abstract baseclass and inherit A and B
  • 使用抽象基类并继承A和B.

  • Use a interface ISearch implemented by SearchA and SearchB that is reference by the base class. It's a classical strategy pattern.
  • 使用由基类引用的SearchA和SearchB实现的接口ISearch。这是一种经典的策略模式。

  • Use generics if you need a typed references to SearchA or SearchB. You probably don't need it.
  • 如果需要对SearchA或SearchB的类型引用,请使用泛型。你可能不需要它。

  • use factories to put things together
  • 用工厂把东西放在一起

#1


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.

我想你想要做的是定义一个由SearchA和SearchB继承的抽象搜索类。使_s成为受保护的搜索类型,将其作为公共属性公开,并在类A中将SearchA分配给它,然后在类B中将SearchA分配给它。然后,GetSearch()将从A返回SearchA,从B返回SearchB,尽管它们都将被输入作为搜索。

#2


I think it is because GetSearch is implemented in A, so it uses the fields from A.

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

Generally, if you use Shadows not in legacy code, you should ask yourself what you are doing wrong.

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

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.

就像在这里一样,这种设计有一种气味。来自A的继承B和来自SearchA的SearchB的两种情况都是滥用继承。它是从狗身上继承一只猫,因为它“大部分都是一样的”。正如Jamie Ide已经说过:你应该使用抽象基类。

Consider to:

  • use a abstract baseclass and inherit A and B
  • 使用抽象基类并继承A和B.

  • Use a interface ISearch implemented by SearchA and SearchB that is reference by the base class. It's a classical strategy pattern.
  • 使用由基类引用的SearchA和SearchB实现的接口ISearch。这是一种经典的策略模式。

  • Use generics if you need a typed references to SearchA or SearchB. You probably don't need it.
  • 如果需要对SearchA或SearchB的类型引用,请使用泛型。你可能不需要它。

  • use factories to put things together
  • 用工厂把东西放在一起