是使用子类中的属性来访问父类更好,还是将父类公开更好?
我有两个班级,一个家长和一个孩子。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于这是两个类之间的
HAS-A
关系,因此我认为您应该保持原样。 您不需要为不需要的东西引入封装代码。下面是一个伪代码示例:
理想情况下,您希望将
Engine
引用为Car
的属性,如下所示:另一种方法是在
Car 中编写额外的代码
将方法包装在Engine
中。 虽然您可以这样做,但这没有多大意义,因为您只需编写从Car
到Engine
的传递方法。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:
Ideally you would want to reference
Engine
as a property ofCar
like so:The alternative would be to write extra code in
Car
to wrap the methods inEngine
. While you can do this it doesn't make much sense as you will be simply writing pass-through methods fromCar
toEngine
.否 - 这违反了德米特法则; 重新考虑接口 - 在什么情况下有人需要访问 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 ;-)
我想说这取决于您想要公开的属性的数量。 但封装始终是一个值得遵循的好规则。 如果 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.