是使用子类中的属性来访问父类更好,还是将父类公开更好?

发布于 2024-07-12 07:05:27 字数 1119 浏览 11 评论 0原文

我有两个班级,一个家长和一个孩子。


Class Test
    Private Test_Text

    Private Sub Class_Initialize()  
        Test_Text = "Hello"
    End Sub  

    Private Sub Class_Terminate()   

    End Sub 

    Public Property Get Text
        Text = Test_Text
    End Property

    Public Property Let Text(ByVal strIn)
        Test_Text = strIn
    End Property
End Class

Class SubTest
    Public SubTest_Test 
    Private SubTest_Interger

    Private Sub Class_Initialize()  
        Set SubTest_Test = New Test
    End Sub  

    Private Sub Class_Terminate()   
        Set SubTest_Test = Nothing
    End Sub

    Public Property Get int
        int = SubTest_Integer
    End Property

    Public Property Let int(ByVal intIn)
        SubTest_Integer = intIn
    End Property    
End Class

因为我已将 SubTest_Test 设为公共,所以我可以通过子类访问它,如下所示,


Set MyTest = New SubTest
MsgBox MyTest.SubTest_Test.Text

这是可以接受的还是我应该将 SubTest_Test 设为私有并在子类中写入属性以访问父类属性?

编辑:我想问题应该是,直接访问父级是否存在任何安全/可用性问题。
我想得越多,从可用性的角度来看,最好对使用子类的任何人隐藏父类。 这样,当您从子类创建对象时,您不必了解有关父类的任何信息。

I have 2 classes, a parent and a child.


Class Test
    Private Test_Text

    Private Sub Class_Initialize()  
        Test_Text = "Hello"
    End Sub  

    Private Sub Class_Terminate()   

    End Sub 

    Public Property Get Text
        Text = Test_Text
    End Property

    Public Property Let Text(ByVal strIn)
        Test_Text = strIn
    End Property
End Class

Class SubTest
    Public SubTest_Test 
    Private SubTest_Interger

    Private Sub Class_Initialize()  
        Set SubTest_Test = New Test
    End Sub  

    Private Sub Class_Terminate()   
        Set SubTest_Test = Nothing
    End Sub

    Public Property Get int
        int = SubTest_Integer
    End Property

    Public Property Let int(ByVal intIn)
        SubTest_Integer = intIn
    End Property    
End Class

Because I have made SubTest_Test public I can access it through the child class like this


Set MyTest = New SubTest
MsgBox MyTest.SubTest_Test.Text

Is this acceptable or should I make SubTest_Test private and write properties in the child class to access the parents properties?

Edit: I guess the question should have been, are there any security/usability issues with accessing the parent directly.
The more I think about it the more I think from a usability standpoint, it is better to hide the parent from anyone using the child class. That way when you are creating an object from the child class, you don't have to know anything about the parent class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

郁金香雨 2024-07-19 07:05:27

由于这是两个类之间的 HAS-A 关系,因此我认为您应该保持原样。 您不需要为不需要的东西引入封装代码。

下面是一个伪代码示例:

class Engine
    [method] start()

class Car
    [property] Engine

理想情况下,您希望将 Engine 引用为 Car 的属性,如下所示:

Car.Engine.start()

另一种方法是在 Car 中编写额外的代码 将方法包装在 Engine 中。 虽然您可以这样做,但这没有多大意义,因为您只需编写从 CarEngine 的传递方法。

Since this is a HAS-A relationship between your two classes then I think you ought to leave it as is. You don't need to introduce encapsulating code for something that does not need it.

Here is a pseudo-code example:

class Engine
    [method] start()

class Car
    [property] Engine

Ideally you would want to reference Engine as a property of Car like so:

Car.Engine.start()

The alternative would be to write extra code in Car to wrap the methods in Engine. While you can do this it doesn't make much sense as you will be simply writing pass-through methods from Car to Engine.

寒冷纷飞旳雪 2024-07-19 07:05:27

否 - 这违反了德米特法则; 重新考虑接口 - 在什么情况下有人需要访问 SubTest 对象中包含的 Test 对象的 Text 属性? 如果这些情况有意义,您需要将 Text 作为 SubTest 中的属性公开。

给定名称,我希望 SubTest 从 Test 继承,从而自动公开 Text 属性,但幸运的是我忘记了 VBSCRIPT,所以我不记得它是否支持继承;-)

no - this violates the Law of Demeter; rethink the interface - under what circumstances would someone need to access the Text property of the enclosed Test object in a SubTest object? If these situations make sense, you'll need to expose Text as a property in SubTest.

Given the names I would have expected SubTest to inherit from Test and thus automatically expose the Text property, but thankfully I have forgotten VBSCRIPT so I can't remember if it even supports inheritance ;-)

誰ツ都不明白 2024-07-19 07:05:27

我想说这取决于您想要公开的属性的数量。 但封装始终是一个值得遵循的好规则。 如果 Text 是您将访问的唯一属性,我很可能将 SubTest_Test 设为私有并包装该属性。

I'd say it depends on the number of properties you want to expose. But encapsulation is always a good rule to follow. If Text is the only property you'll be accessing, I'd most likely make SubTest_Test private and wrap the property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文