vb.net 将函数声明为私有与公开之间的实际区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15513546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the practical difference between declaring a function private vs public?
提问by Blunderfest
So as I've been reading/learning about classes and the methods within them I've found very little about the practical differences between declaring a method as public versus private.
因此,当我一直在阅读/学习类及其中的方法时,我几乎没有发现将方法声明为 public 与 private 之间的实际差异。
I know that the difference is a private class can only be accessed within the class, while a public method can be accessed from code outside the class (other classes, functions). But what I really want to know is:
我知道区别在于私有类只能在类内访问,而公共方法可以从类外的代码(其他类、函数)访问。但我真正想知道的是:
- Why would you want/not want to declare it one way or another when deploying an application?
- Are there best practices that can guide whether to declare a method public vs private?
- 为什么在部署应用程序时想要/不想以一种或另一种方式声明它?
- 是否有最佳实践可以指导是将方法声明为 public 还是 private?
Also, I don't know if it matters, but I am learning primarily VB.Net and C# in a web application environment, so specifics to that would help.
另外,我不知道这是否重要,但我主要是在 Web 应用程序环境中学习 VB.Net 和 C#,因此这方面的细节会有所帮助。
回答by Michael Rodrigues
Encapsulationmeans that you should think of each class as a machine that provides a service. For example, a chair allows you to sit on it, or a lawnmower allows you to cut your lawn.
封装意味着您应该将每个类视为提供服务的机器。例如,椅子可以让您坐在上面,或者割草机可以让您修剪草坪。
The privatemethods pertain to the machine's internal workings. In contrast, the publicmethods relate to how you (other classes) interact with the machine.
该私有方法涉及到机器的内部工作。相反,公共方法与您(其他类)如何与机器交互有关。
Example one:Chair...
例一:椅子...
When sitting on a chair, you don't need to know volume of stuffing or the number of staples, you basically need to know whether or not it's occupied and if it's stable.
坐在椅子上,你不需要知道馅料的体积或订书钉的数量,你基本上需要知道它是否被占用,是否稳定。
- Public methods: IsStable, IsOccupied, Sit
- Private methods: CalculateStuffingVolume, CountNumberOfStaples
- 公共方法:IsStable、IsOccupied、Sit
- 私有方法:CalculateStuffingVolume、CountNumberOfStaples
Example two:Lawnmower...
示例二:割草机...
For the lawnmower, you need to know if it has enough fuel (or is plugged in), if the blades are sharp, and be able to turn it on.
对于割草机,您需要知道它是否有足够的燃料(或已插入),刀片是否锋利,以及是否能够打开它。
- Public methods: GetFuelLevel, IsBladesSharp, TurnOn, TurnOff
- Private methods: Combust, etc, Too many to imagine.
- 公共方法:GetFuelLevel、IsBladesSharp、TurnOn、TurnOff
- 私有方法:Combust 等,太多难以想象。
Conclusion:
结论:
So when you're developing all you will see is...
所以当你在开发时,你会看到的是......
Example one:Chair.Sit, Chair.IsStable and Chair.IsOccupied
示例一:Chair.Sit、Chair.IsStable 和 Chair.IsOccupied
or
或者
Example two:Lawnmower.GetFuelLevel, Lawnmower.IsBladesSharp, Lawnmower.TurnOn, LawnMower.TurnOff
示例二:Lawnmower.GetFuelLevel、Lawnmower.IsBladesSharp、Lawnmower.TurnOn、LawnMower.TurnOff
As a developer, you will not have to think about number of threads in the uphosltry, the colour of the fuel cap, the number of RPM of the blades or whether the chair is glued or stapled together. This distinction makes it much easier to put your application together without being swamped in detail. Additionally, it allows programmers to expose only necessary information which adds a level of security. As John mentioned, this prevents the Person class from calling Lawnmower.Combust(fuel) when they're not supposed to.
作为开发人员,您不必考虑室内装潢中的螺纹数量、燃料盖的颜色、刀片的 RPM 数量或椅子是否粘在一起或钉在一起。这种区别使您可以更轻松地将您的应用程序组合在一起,而不会被细节所淹没。此外,它允许程序员只公开必要的信息,这增加了安全级别。正如约翰提到的,这可以防止 Person 类在不应该调用 Lawnmower.Combust(fuel) 时调用它们。
回答by John Saunders
If the method is private, then you don't have to think about outside classes calling it incorrectly.
如果该方法是私有的,那么您不必考虑外部类错误地调用它。
One of the benefits of this is that it allows for a separation between the interface to your class (the public parts of it), and the implementationof it. If nobody knows how you implemented your class (that is, if there is no code that depends on how you implemented it), then you're free to change the way you implemented it without breaking any other code.
其中一个这样的好处是,它允许接口之间的类(它的公共部分)的分离,并实现它。如果没有人知道你是如何实现你的类的(也就是说,如果没有取决于你如何实现它的代码),那么你可以自由地改变你实现它的方式而不会破坏任何其他代码。
回答by automatix
It's one of the most importand principles of the object-oriented programming -- encapsulation.
面向对象编程最重要的原则之一——封装。
A class usually provides a (public) interface. E.g. (pseudocode):
一个类通常提供一个(公共)接口。例如(伪代码):
class Rectangle {
private length;
private width;
public getPerimeter() {
// return calculatePerimeterOld();
return calculatePerimeterNew();
}
private calculatePerimeterOld() {
// old variant
}
private calculatePerimeterNew() {
// Here the perimeter is caltulated.
// so: perimeter = 2 * (length + width)
// or so: perimeter = 2 * (length) + 2 * (width)
// or maybe so: perimeter = length + width + length + width - (0.5 * length) + 2 * 0.25 * length)
return perimeter;
}
}
I can change my private methods however I want. I can rename or replace them with oher methods. But my public interface will stay the same -- and it has to stay the same, because it's a contract I sign, when I'm defining a mehod as public. Everything, what is signed as privateis my "blackbox" and I can do there whatever I want.
我可以随意更改我的私有方法。我可以用其他方法重命名或替换它们。但是我的公共界面将保持不变——它必须保持不变,因为这是我签署的合同,当我将方法定义为public. 一切,签名为private我的“黑匣子”,我可以在那里为所欲为。
It's the main reason. Another reason is, that we should not provide the user(-s) of our class with methods/informations, they don't need. To much (information) is not good.
这是主要原因。另一个原因是,我们不应该为我们的类的用户(-s)提供方法/信息,他们不需要。太多(信息)不好。
回答by Derek Tomes
If you're developing a one off website maintained just by you, you may find the concept of public and private functions unnecessary.
如果您正在开发一个由您自己维护的一次性网站,您可能会发现公共和私人功能的概念是不必要的。
However, if you're delivering software to other people, and they're building on top of your software, it is an absolutely critical concept.
但是,如果您向其他人交付软件,并且他们构建在您的软件之上,那么这绝对是一个至关重要的概念。
Think of it like a physical machine that has a thousand knobs and switches inside it, but is contained inside a pretty case with only a few clearly labelled knobs on the outside.
把它想象成一台内部有上千个旋钮和开关的物理机器,但它被装在一个漂亮的箱子里,外面只有几个明确标记的旋钮。
- The public functions and methods are the ones you create for external parties to interact with your software.
- The private functions and methods are the ones you create for your software to interact with itself.
- 公共函数和方法是您为外部各方与您的软件进行交互而创建的函数和方法。
- 私有函数和方法是您为软件创建以与自身交互的函数和方法。
But, again, if it's a one-off website maintained by a single developer, these differences are less important.
但是,如果它是由单个开发人员维护的一次性网站,那么这些差异就不那么重要了。

