I have 2 classes, a parent and a child.
我有2个班,父母和孩子。
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
因为我已经公开了SubTest_Test,所以我可以像这样通过子类访问它
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?
这是可接受的,还是应该让SubTest_Test私有并在子类中写入属性来访问父属性?
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.
编辑:我想问题应该是,直接访问父母是否有任何安全/可用性问题。我越是想到它,从可用性的角度来看,我越是认为使用子类隐藏父母。这样,当您从子类创建对象时,您不必了解有关父类的任何信息。
3 个解决方案
#1
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.
不 - 这违反了得墨忒耳法则;重新考虑界面 - 在什么情况下有人需要访问SubTest对象中包含的Test对象的Text属性?如果这些情况有意义,则需要在SubTest中将Text公开为属性。
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 ;-)
鉴于名称,我希望SubTest继承Test,从而自动公开Text属性,但幸运的是我忘记了VBSCRIPT,所以我不记得它是否支持继承;-)
#2
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.
既然这是你们两个班级之间的HAS-A关系,那么我认为你应该保持原样。您不需要为不需要它的东西引入封装代码。
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:
理想情况下,您可能希望引用Engine作为Car的属性,如下所示:
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
.
另一种方法是在Car中编写额外的代码来包装Engine中的方法。虽然你可以做到这一点没有多大意义,因为你只是简单地将传递方法从Car写入Engine。
#3
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.
我会说这取决于你想要公开的属性数量。但封装始终是一个很好的规则。如果Text是您将访问的唯一属性,我很可能将SubTest_Test设为私有并包装该属性。
#1
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.
不 - 这违反了得墨忒耳法则;重新考虑界面 - 在什么情况下有人需要访问SubTest对象中包含的Test对象的Text属性?如果这些情况有意义,则需要在SubTest中将Text公开为属性。
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 ;-)
鉴于名称,我希望SubTest继承Test,从而自动公开Text属性,但幸运的是我忘记了VBSCRIPT,所以我不记得它是否支持继承;-)
#2
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.
既然这是你们两个班级之间的HAS-A关系,那么我认为你应该保持原样。您不需要为不需要它的东西引入封装代码。
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:
理想情况下,您可能希望引用Engine作为Car的属性,如下所示:
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
.
另一种方法是在Car中编写额外的代码来包装Engine中的方法。虽然你可以做到这一点没有多大意义,因为你只是简单地将传递方法从Car写入Engine。
#3
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.
我会说这取决于你想要公开的属性数量。但封装始终是一个很好的规则。如果Text是您将访问的唯一属性,我很可能将SubTest_Test设为私有并包装该属性。