为什么C#不提供C ++风格的'朋友'的关键字? [英] Why does C# not provide the C++ style 'friend' keyword?

查看:149
本文介绍了为什么C#不提供C ++风格的'朋友'的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助 C ++的朋友关键字允许 A级指定 B类作为它的朋友。这让 B类访问私人 / 保护成员的 A级

The C++ friend keyword allows a class A to designate class B as its friend. This allows Class B to access the private/protected members of class A.

我从来没读过什么,为什么这被排除在外的C#(和VB.NET)。大多数回答这个较早的StackOverflow问题似乎在说这是一个C ++的有效组成部分,也有使用它很好的理由。在我的经验,我不得不同意。

I've never read anything as to why this was left out of C# (and VB.NET). Most answers to this earlier StackOverflow question seem to be saying it is a useful part of C++ and there are good reasons to use it. In my experience I'd have to agree.

另一个<一个href=\"http://stackoverflow.com/questions/200079/inheritance-trees-and-protected-constructors-in-c#200117\">question在我看来,要真正问如何做类似朋友在C#应用程序的东西。虽然答案一般在嵌套类旋转,它似乎并不像使用朋友优雅关键字。

Another question seems to me to be really asking how to do something similar to friend in a C# application. While the answers generally revolve around nested classes, it doesn't seem quite as elegant as using the friend keyword.

原来设计模式一书经常使用它整个的例子。

The original Design Patterns book uses it regularly throughout its examples.

因此​​,在总结,为什么朋友从C#缺,什么是用C#模拟它的最佳实践的方式(或方法)?

So in summary, why is friend missing from C#, and what is the "best practice" way (or ways) of simulating it in C#?

(顺便说一句,在内部关键字的的同样的事情,它允许的所有的内部类整个组件访问内部成员,而朋友允许你给某个类的完全访问一个的其他类)

(By the way, the internal keyword is not the same thing, it allows all classes within the entire assembly to access internal members, while friend allows you to give a certain class complete access to exactly one other class)

推荐答案

在编程有朋友更是或多或少被认为是脏,容易被滥用。它打破类之间的关系,破坏了面向对象语言的一些基本属性。

Having friends in programming is more-or-less considered "dirty" and easy to abuse. It breaks the relationships between classes and undermines some fundamental attributes of an OO language.

话虽这么说,这是一个很好的功能,我已经用它很多次,我自己用C ++;并想在C#中使用它。但我敢打赌,因为C#的纯OOness(相比于C ++的伪OOness)MS决定,因为Java已经没有朋友的关键字C#不应该是(开个玩笑;))

That being said, it is a nice feature and I've used it plenty of times myself in C++; and would like to use it in C# too. But I bet because of C#'s "pure" OOness (compared to C++'s pseudo OOness) MS decided that because Java has no friend keyword C# shouldn't either (just kidding ;))

在一个严重的注意:内部并不像朋友一样好,但它确实把工作做好。请记住,这是罕见的,你会被分发您code到第三方开发者,虽然没有一个DLL;所以只要你和你的团队了解内部类和它们的使用,你应该罚款。

On a serious note: internal is not as good as friend but it does get the job done. Remember that it is rare that you will be distributing your code to 3rd party developers not though a DLL; so as long as you and your team know about the internal classes and their use you should be fine.

修改让我澄清一下朋友的关键字是如何破坏了面向对象的。

EDIT Let me clarify how the friend keyword undermines OOP.

private和protected变量和方法也许是OOP中最重要的部分之一。对象可以保存数据或逻辑,只有他们可以使用这个想法让你写你的功能独立于环境的实现 - 而您的环境不能改变的状态信息,它不适合处理。通过朋友你是两个阶级的实现耦合在一起 - 这是更糟糕那么,如果你只是加上它们的接口。

Private and protected variables and methods are perhaps one of the most important part of OOP. The idea that objects can hold data or logic that only they can use allows you to write your implementation of functionality independent of your environment - and that your environment cannot alter state information that it is not suited to handle. By using friend you are coupling two classes' implementations together - which is much worse then if you just coupled their interface.

这篇关于为什么C#不提供C ++风格的'朋友'的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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