本文介绍了如何决定一个方法是私有的,保护的,内部或公开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个类,其中一些方法不会造成任何伤害,如果他们公开为公众。但是,他们可以是专用为好,因为它们将只用于从同一类在我的项目。

I am designing a class where some methods won't cause any harm if they are exposed as public. But they can be private as well, since they will be used only from the same class in my project.

让它们公开具有以下优点:

Making them public has the following advantages:

  1. 在可测,而不访问的需求单位。
  2. 灵活性。

让它们民营具有以下优点:

Making them private has the following advantages:

  1. 在公共文档简化。
  2. 在一些不知名的虫子不会暴露。

这是在这种情况下的一般准则?

Which are the general guidelines in this case?

推荐答案

哦,拜托请仔细阅读通道。 06 code完成2由史蒂夫·麦康奈尔,如果你有机会获得它。这将完全回答你的问题。

Oh, please please read Ch. 06 Of Code Complete 2 by Steve McConnell, if you have access to it. That will answer your question perfectly.

在一般情况下,如果该方法在类的整体假面千篇一律,把它公开。更在技术上,尽量不要打破抽象。我会给出一个例子,但我不知道你的工作范围内,因此例子可能是无关紧要的。

In general, If the Method fits in the overall "Persona" of the class, make it public. More technically, try not to break the abstraction. I would have given an example, but I do not know the context of your work and so examples might be irrelevant.

如果你不需要它,也没有必要做一个方法公开。

If you do not need it, there is no need to make a Method public.

有关测试,+1约翰·桑德斯。

For testing, +1 to John sanders.

但我真的不能解释你在这里为史蒂夫在CC2解释。

But I really can not explain you here as Steve has explained in CC2.

我希望在Satckoverflow其确定书后的参考? (请评论。)

I hope its OK to post Book references on Satckoverflow? (Please comment.)

这篇关于如何决定一个方法是私有的,保护的,内部或公开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 00:18