封装 [英] Encapsulation

查看:89
本文介绍了封装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个单独的类,并且希望一个类的成员能够访问另一个类的私有成员。什么是惯用的C#

解决方案?


我相信C ++会使用朋友。


-

飞行青蛙顾问Jon D Harrop博士
http://www.ffconsultancy.com/products/?u

推荐答案

2008年5月23日星期五21 :07:16 -0700,Jon Harrop< jo*@ffconsultancy.com>

写道:
On Fri, 23 May 2008 21:07:16 -0700, Jon Harrop <jo*@ffconsultancy.com>
wrote:

我有两个单独的班级希望一个班级的成员能够获得另一个班级的私人成员的
。什么是惯用的C#

解决方案?


我相信C ++会使用朋友。
I have two separate classes and would like the members of one class to
have
access to the private members of another class. What is the idiomatic C#
solution to this?

I believe C++ would use "friends".



它在C#中不存在。


你可以接近内部关键词。在那个访问修饰符中,

同一个程序集中的每个类都相当于一个

friend,除了私有和受保护的成员仍然是

隐藏。你会用内部的您真正想要分享的成员。


或者,嵌套类可以访问包含类的

的私有成员。但恕我直言,如果

嵌套类作为嵌套类真的有意义,那只是一个合适的解决方案。我不会只用

来解决一些可访问性问题。


Pete

It doesn''t exist in C#.

You can come close with the "internal" keyword. In that access modifier,
every class within the same assembly would be equivalent to being a
"friend", except that private and protected members would still be
hidden. You''d use "internal" on the members that you really want to share.

Alternatively, nested classes have access to private members of the
containing class(es). But IMHO that''s only an appropriate solution if the
nested class really makes sense as a nested class. I wouldn''t use it just
to address some accessibility issue.

Pete


Jon Harrop写道:
Jon Harrop wrote:

我有两个单独的类,并且希望一个类的成员能够访问另一个类的私有成员

类。什么是惯用的C#

解决方案?


我相信C ++会使用朋友。
I have two separate classes and would like the members of one class to have
access to the private members of another class. What is the idiomatic C#
solution to this?

I believe C++ would use "friends".



典型的C#解决方案是将类放在相同的

汇编中并使用internal。可访问性。


与C ++朋友最相似的是如果他们使用

" internal"可访问性和不同的程序集,但

使用InternalsVisibleTo属性 - 请参阅:

http://msdn.microsoft.com/en-us/libr...attribute.aspx


Arne

The typical C# solution would be to put the classes in same
assembly and use "internal" accessibility.

The most similar to C++ friend would be if they use
"internal" accessibility and are in different assemblies, but
use the InternalsVisibleTo attribute - see details at:

http://msdn.microsoft.com/en-us/libr...attribute.aspx

Arne


Arne Vajh?j写道:
Arne Vajh?j wrote:

Jon Harrop写道:
Jon Harrop wrote:

>我有两个独立的课程,希望一个班级的成员能够访问私人会员另一堂课。什么是对此的惯用C#解决方案?

我相信C ++会使用朋友。
>I have two separate classes and would like the members of one class to
have access to the private members of another class. What is the
idiomatic C# solution to this?

I believe C++ would use "friends".



典型的C#解决方案是将类放在相同的

汇编中并使用internal。可访问性。


The typical C# solution would be to put the classes in same
assembly and use "internal" accessibility.



这只限制了其他代码的百万行的可访问性

碰巧在同一个程序集中,这是一个弱约束至于

没有实际用途。


-

Jon D Harrop博士,飞蛙咨询公司
http://www.ffconsultancy.com/products/?u


这篇关于封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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