令人困惑的阶级关系 [英] Confusing class relationships

查看:76
本文介绍了令人困惑的阶级关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我有一个带有一些公共变量的A类,这些变量将由其他类(Forms)访问,因此这些类创建A的实例.A类与其他类之间的关系应该是什么?

我正在使用C#.Net

Hi I have a class A with some public variables, these variables are to be accessed by other classes (Forms), so those classes create instances of A. What should be the relationship between class A and other classes?
I am working in C#.Net

推荐答案

首先,变量不应该是公共的 - 它们通常应该封装在Properties中。



表单与它创建的任何类实例之间不存在任何关系 - 或者至少没有正式关系 - 除了表单之间存在关系之外以及它在文本框中显示的字符串。表单可以创建实例,但它不以任何方式与字符串相关,除了它使用它来设置TextBox属性的值。您的类与字符串相同,除非它以某种方式从Control派生,在这种情况下可以显示它,然后表单可以是类控件实例的Parent控件。
First off, variables should not be public - they should normally be encapsulated in Properties instead.

There is no relationship that exists between a form and any class instances it creates - or at least no formal relationship - any more than there is a relationship between your form and a string it displays in a text box. The form may create the instance, but it isn't "related" to the string in any way, other than it uses it to set the value of a TextBox property. Your class is the same as the string, unless it is derived from Control in some way, in which case it can be displayed, and then the form can be the Parent control for the instance of your class control.


作为面向对象的编程概念,永远不要暴露公共变量。而是将它们声明为私有,并使用公共方法或属性从其他类访问。​​



根据解决方案,您的A和其他类之间不需要关系。它只是在Form类中创建一个A对象。
As Per Object Oriented programming concepts, never expose public variables. instead declare them as private and use public methods or properties to access from other classes.

And as per solution there is no relation required between your A and other classes. It is just creating an object of A in Form class.


一个表单是一个类实例,这是一个对象。当它创建类A 的实例时,它将成为该对象的创建者或所有者。这是与对象的具有关系。
A Form is a class instance, that is an object. When it creates an instance of class A then it becomes the 'creator' or the 'owner' of that object. That is a 'has' relationship with the object.


这篇关于令人困惑的阶级关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆