如何让基类方法访问其派生类的阴影属性?

时间:2022-11-25 00:28:34

Given this example:

鉴于这个例子:

Imports System

Public Module Module1
Public Sub Main()
    Console.WriteLine("Expect 'WheelValue' here.")
    Dim car as New Car()
    car.DoSomething()

    Console.WriteLine("Expect 'FordWheelValue' here.")
    Dim ford as New Ford()
    ford.DoSomething()
End Sub

Public Class Car
    Public Sub DoSomething()
        Console.WriteLine("Car.DoSomething()")
        Console.WriteLine("Wheel=" + Wheel.Value)
        Console.WriteLine(Environment.NewLine)
    End Sub
    Public Property Wheel As New Wheel()
End Class

Public Class Ford
    Inherits Car
    Public Shadows Property Wheel As New FordWheel()        
End Class

Public Class Wheel
    Public Value As String = "WheelValue"
End Class

Public Class FordWheel
    Public Value As String = "FordWheelValue"
End Class
End Module

Calls to Ford.DoSomething() use the base class's property Wheel.Value. In the interest of not duplicating code, is there a way using VB to write this cleanly so that these calls to Ford.DoSomething() instead grab the instance of Ford's property FordWheel.Value? It seems like what I might need is a language feature like "Shadows Overrides" - but this isn't supported in VB from what I can tell.

调用Ford.DoSomething()使用基类的属性Wheel.Value。为了不复制代码,有没有办法使用VB来干净地编写它,以便这些调用Ford.DoSomething()而不是获取Ford的属性FordWheel.Value的实例?看起来我可能需要的是像“Shadows Overrides”这样的语言功能 - 但是从我所知道的VB中不支持这种功能。

3 个解决方案

#1


0  

You could redefine the type of Wheel you are using inside the Ford class. Wheel doesn't need to be defined in Ford anymore, since the signature would be the same. FordWheel is now a descendant of Wheel and overrides the Value property.

您可以重新定义您在Ford类中使用的Wheel类型。由于签名是相同的,因此不再需要在福特定义车轮。 FordWheel现在是Wheel的后代并且超越了Value属性。

Public Class Car

    Public Sub New()
        Wheel = New Wheel()
    End Sub

    Public Sub DoSomething()
        Console.WriteLine("Car.DoSomething()")
        Console.WriteLine("Wheel=" + Wheel.Value)
        Console.WriteLine(Environment.NewLine)
    End Sub

    Public Property Wheel As Wheel
End Class

Public Class Ford
    Inherits Car

    Public Sub New()
        Wheel = New FordWheel()
    End Sub

End Class

Public Class Wheel
    Public Overridable Property Value As String = "WheelValue"
End Class

Public Class FordWheel
    Inherits Wheel

    Public Overrides Property Value As String = "FordWheelValue"
End Class

#2


0  

You want to make the base class property overridable:

您希望使基类属性可覆盖:

Public Class Car
    Public Sub DoSomething()
        Console.WriteLine("Car.DoSomething()")
        Console.WriteLine("Wheel=" + Wheel.Value)
        Console.WriteLine(Environment.NewLine)
    End Sub
    Public Overridable Property Wheel As New Wheel()
End Class

Then when you declare a derived class, mark your property as "taking over" the existing one:

然后,当您声明派生类时,将您的属性标记为“接管”现有属性:

Public Class Ford
    Inherits Car
    Public Overrides Property Wheel As Wheel = New FordWheel()
End Class

Note: For this to work, you need to change your FordWheel type to inherit from Wheel type. You also just need to set the value of the fordwheel instead of creating a "shadowed" which is what would happen if you just inherited directly:

注意:为此,您需要将FordWheel类型更改为从Wheel类型继承。您还需要设置fordwheel的值而不是创建“shadowed”,如果您直接继承,将会发生这种情况:

Public Class FordWheel
    Inherits Wheel

    Public Sub New()
        Value = "FordWheel"
    End Sub

End Class

And as Saragis said, you should just set the Wheel property to New FordWheel directly in the constructor of Ford, just like I did above with FordWheel

正如Saragis所说,你应该直接在福特的构造函数中将Wheel属性设置为New FordWheel,就像我上面用FordWheel做的那样

#3


0  

As both of those who have answered points out , you need to make use of the oop concept of Generalization as FordWheel is a Wheel. And the Following code is just the resemblance of a popular design pattern called Bridge Pattern where implementations and concretions are separated out.

正如两位已经回答的人所指出的那样,你需要利用泛化概念,因为FordWheel是一个*。以下代码只是一种名为Bridge Pattern的流行设计模式的相似之处,其中实现和结构被分离出来。

Public Class Car

公共汽车

Public Sub New()
    Wheel = New Wheel()
End Sub

Public Property Wheel As Wheel
End Class

Public Class Ford
     Inherits Car

    Public Sub New()
        Wheel = New FordWheel()
    End Sub

End Class

Public Class Wheel
    Public Overridable Property Value As String = "WheelValue"
End Class

Public Class FordWheel
    Inherits Wheel

    Public Overrides Property Value As String = "FordWheelValue"
End Class

Public Class Executor
   Public Sub Main()
      Console.WriteLine("Expect 'WheelValue' here.")
      Dim car as New Car()
      Console.WriteLine(car.Wheel)

      Console.WriteLine("Expect 'FordWheelValue' here.")
      Dim ford as New Ford()
      Console.WriteLine(ford.Wheel)
   End Sub
End Class

Sorry for wrong syntax if any , As i'm a Java Developer.

对不起,如果有错误的语法,因为我是一个Java开发人员。

#1


0  

You could redefine the type of Wheel you are using inside the Ford class. Wheel doesn't need to be defined in Ford anymore, since the signature would be the same. FordWheel is now a descendant of Wheel and overrides the Value property.

您可以重新定义您在Ford类中使用的Wheel类型。由于签名是相同的,因此不再需要在福特定义车轮。 FordWheel现在是Wheel的后代并且超越了Value属性。

Public Class Car

    Public Sub New()
        Wheel = New Wheel()
    End Sub

    Public Sub DoSomething()
        Console.WriteLine("Car.DoSomething()")
        Console.WriteLine("Wheel=" + Wheel.Value)
        Console.WriteLine(Environment.NewLine)
    End Sub

    Public Property Wheel As Wheel
End Class

Public Class Ford
    Inherits Car

    Public Sub New()
        Wheel = New FordWheel()
    End Sub

End Class

Public Class Wheel
    Public Overridable Property Value As String = "WheelValue"
End Class

Public Class FordWheel
    Inherits Wheel

    Public Overrides Property Value As String = "FordWheelValue"
End Class

#2


0  

You want to make the base class property overridable:

您希望使基类属性可覆盖:

Public Class Car
    Public Sub DoSomething()
        Console.WriteLine("Car.DoSomething()")
        Console.WriteLine("Wheel=" + Wheel.Value)
        Console.WriteLine(Environment.NewLine)
    End Sub
    Public Overridable Property Wheel As New Wheel()
End Class

Then when you declare a derived class, mark your property as "taking over" the existing one:

然后,当您声明派生类时,将您的属性标记为“接管”现有属性:

Public Class Ford
    Inherits Car
    Public Overrides Property Wheel As Wheel = New FordWheel()
End Class

Note: For this to work, you need to change your FordWheel type to inherit from Wheel type. You also just need to set the value of the fordwheel instead of creating a "shadowed" which is what would happen if you just inherited directly:

注意:为此,您需要将FordWheel类型更改为从Wheel类型继承。您还需要设置fordwheel的值而不是创建“shadowed”,如果您直接继承,将会发生这种情况:

Public Class FordWheel
    Inherits Wheel

    Public Sub New()
        Value = "FordWheel"
    End Sub

End Class

And as Saragis said, you should just set the Wheel property to New FordWheel directly in the constructor of Ford, just like I did above with FordWheel

正如Saragis所说,你应该直接在福特的构造函数中将Wheel属性设置为New FordWheel,就像我上面用FordWheel做的那样

#3


0  

As both of those who have answered points out , you need to make use of the oop concept of Generalization as FordWheel is a Wheel. And the Following code is just the resemblance of a popular design pattern called Bridge Pattern where implementations and concretions are separated out.

正如两位已经回答的人所指出的那样,你需要利用泛化概念,因为FordWheel是一个*。以下代码只是一种名为Bridge Pattern的流行设计模式的相似之处,其中实现和结构被分离出来。

Public Class Car

公共汽车

Public Sub New()
    Wheel = New Wheel()
End Sub

Public Property Wheel As Wheel
End Class

Public Class Ford
     Inherits Car

    Public Sub New()
        Wheel = New FordWheel()
    End Sub

End Class

Public Class Wheel
    Public Overridable Property Value As String = "WheelValue"
End Class

Public Class FordWheel
    Inherits Wheel

    Public Overrides Property Value As String = "FordWheelValue"
End Class

Public Class Executor
   Public Sub Main()
      Console.WriteLine("Expect 'WheelValue' here.")
      Dim car as New Car()
      Console.WriteLine(car.Wheel)

      Console.WriteLine("Expect 'FordWheelValue' here.")
      Dim ford as New Ford()
      Console.WriteLine(ford.Wheel)
   End Sub
End Class

Sorry for wrong syntax if any , As i'm a Java Developer.

对不起,如果有错误的语法,因为我是一个Java开发人员。