如何在C#中获得更多的封装?
我最近大约完全学习了 C#。然而,在 C++ 中,当将对象传递给方法时,我可以使用 const 修饰符实现更好的封装。但在C#中,可以通过引用参数修改对象,例如Graphics
对象上的Dispose
。
有时我需要更多的封装。属性可以给出更好的解决方案。但是,我发现,例如,不允许修改 Form.Location
属性甚至其 X
成员。虽然 Form.Location
是 get; set;
属性,其成员不能修改。我尝试用 Point
{get; 来做到这一点set;}
属性,但我仍然可以修改 X
成员。
正如前面提到的,当诸如 Dispose 之类的方法是对象时,这也是一个问题。
如何在C#中实现更多的封装?
I have recently learned C# fully, approximately. However, in C++ I could implement a better encapsulation when passing objects to methods, with const
modifier. But in C#, objects can be modified through the reference parameter, such as Dispose
on Graphics
object.
I need sometimes more encapsulation. Properties can give a better solution. However, I have found that, for example, the modification to Form.Location
property or even to its X
member is not allowed. Although Form.Location
is get; set;
property, its members can not be modified. I tried to do as it with a Point
{get; set;}
property but I can still modify X
member.
This is also an issue when a method for example Dispose
s an object, as eariler mentioned.
How can I implement more encapsulation in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能使用
Form.Location
的X
属性,因为Form.Location
是返回值类型的属性(点
)。一旦您访问Form.Location
,您就会获得该位置的副本;因此,更改此副本中的某些内容是没有意义的。由于this.Location.X = ...
是一个明显的错误,C# 编译器会阻止您这样做。但是,您可以替换
Form.Location
的完整值,因为它的属性设置器是public
:或者,
回到您原来的问题:如果您愿意要使对象可访问,但不希望它被修改,您需要自己封装它。例如,如果您希望类的使用者能够在
Graphics
对象上进行Draw
,但不希望他们调用Dispose
,只是无法直接公开 Graphics 对象。相反,您需要包装允许消费者调用的每个方法:You cannot the
X
property ofForm.Location
, becauseForm.Location
is a property that returns a value type (Point
). As soon as you accessForm.Location
, you get a copy of the location; thus, changing something in this copy is meaningless. Sincethis.Location.X = ...
is an obvious mistake, the C# compiler prevents you from doing it.You can, however, replace the complete value of
Form.Location
, since its property setter ispublic
:or, alternatively,
To get back to your original question: If you want to make an object accessible, but do not want it to be modified, you will need to encapsulate it yourself. For example, if you want the consumers of your class to be able to
Draw
on yourGraphics
object but do not want them to callDispose
, just cannot expose theGraphics
object directly. Instead, you need to wrap every method that the consumer is allowed to call:这里有两篇文章讨论了为什么 C# 中没有“const”修饰符:
http://blogs.msdn.com/b/ericgu/archive/2004/04/22/118238.aspx
http://blogs.msdn.com/b/slippman/archive /2004/01/22/61712.aspx
将类设计为 immutable 有时可以解决您的问题,但并不总是一个选项,因为它只适用于“类”级别,而不是方法级别。有时您可能必须公开类的成员变量,该变量不是不可变类型,因此可以从外部修改。
最佳选择:接受这一点,在实践中,常量的重要性常常被高估。
Here are two articles discussing why there is no "const" modifier in C#:
http://blogs.msdn.com/b/ericgu/archive/2004/04/22/118238.aspx
http://blogs.msdn.com/b/slippman/archive/2004/01/22/61712.aspx
Designing classes as immutable is sometimes a solution to your problem, but is not always an option, since it does only work on the "class" level, not on the method level. And sometimes you may have to expose a member variable of your class which is not of an immutable type and could therefore be modified from outside.
Best option: live with that, in practice the importance of const-ness is often overrated.
你需要 Point { get; }。这是允许修改的
set
。You need
Point { get; }
. It's theset
that allows modification.