从两个不同的类创建一个对象两次是不是很糟糕?
我有一个主窗体,当用户单击按钮时,它会弹出气球提示。气球提示是在我的 BalloonTip 类的主窗体类中实例化的对象。然后我有第二个设置表格。当用户单击设置表单中的某些内容时,也会出现气球提示。目前,我在 Main 类和 SettingsForm 类中实例化了一个气球提示对象。我的两个问题是:
- 有没有更合适的方法来处理这种情况?
- 如果在每个类中创建一个对象两次 1,如果对象具有相同的名称(即 objectBalloon),是否会在编译器中引起任何歧义?
I have a main form where when a user clicks a button it brings up a balloon tip. The balloon tip is an object instantiated within my Main form class from my BalloonTip class. I then have a second form for settings. When the user clicks something in the settings form a balloon tip occurs as well. I currently have a balloontip object instantiated in my Main class as well as my SettingsForm class. My two questions are:
- Is there a more appropriate way to deal with this type of situation?
- If creating an object twice 1 in each class, will it cause any kind of ambiguity in the compiler if the objects have the same name (i.e. objectBalloon)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你实例化一个对象时,它总是在一定的范围内。
例如:
会给你两个不同的 BalloonTip 实例,它们都被称为“b”,但它们都只在声明它们的函数范围内有效。
您应该将类定义视为可以实例化多个对象的蓝图。在一个范围内,您可以有多个实例,但它们应该具有不同的名称。
当范围不重叠时,您可以使用相同的名称来指向不同的实例。
您还可以将实例传递给另一个方法,并且在该函数中您可以通过另一个名称引用该实例。
When you instantiate an object, this is always within a certain scope.
So for example:
Would give you two different instances of BalloonTip, which are both called 'b' but they are both only valid within the scope of the function in which they are declared.
You should see a class definition as a blueprint from which multiple objects can be instantiated. In one scope you can have several instances but they should have a different name.
When scopes don't overlap you can use the same name to point to a different instance.
You can also pass an instance to another method and in that function you could refer to the instance by another name.