为什么要“私有"?面向对象的方法? [英] Why "private" methods in the object oriented?

查看:196
本文介绍了为什么要“私有"?面向对象的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是哎呀,这是一个非常基本的概念.但是我仍然无法摆脱困境.我理解了为什么成员变量是私有的,所以类用户不能通过设置无效值来滥用它.

但是这如何适用于这些方法?

解决方案

很多好的答案,但是也许我从一个自学成才的Java程序员那里又得到了一个好答案,因为我独自一人经历了所有的痛苦;)

将类看作是从外部看到的东西,而不是内部看到的东西.如果您从外面看一堂课,您会看到什么?

再次以时钟为例,时钟可以为您提供有关当前时间的信息,并且可以将其设置为显示正确的时间.

>

因此,从外部看事物,时钟是一台可以完成这两项任务的机器.我们称之为公共方法.

但是作为时钟的构造函数,我们知道,在任何时间操作之前,我们都必须在显示器上从23切换到11(这是这种时钟),因此我们必须在内部进行一些重新排列.在两种情况下,将23更改为11对我们来说都很好-设置时钟并显示当前时间-但我们可以在侧面" 进行操作,因为用户不必了解所有情况这么复杂的数学.这些是私有方法!

因此我们的Clock类可以具有两个公共方法(showTimesetTime),这些方法都是用户 想要 查看,以及私有方法(recountTime),该方法为这些公共方法提供功能,并且用户 不想要 看.

因此,一方面,您应该牢记私有是将来的程序员不会使用您的代码重新实现和访问的(如上面的答案中所指出的).但是私有也意味着侧边完成的事情,因此用户看不到.这就是为什么我们将公共方法称为公共接口的原因-这是用户从外部看到的全部内容.

对我来说,写下用户(实际用户和其他类)将对我的类(公共接口与用户)所做的一切都是有帮助的(我是自学成才的,所以也许这不是一种非常流行的方法...)只是公共方法的签名),然后编写私有方法的签名,实施者我将使用该方法来实现承诺提供给我的用户的公共目标,然后用代码来实现它.

请记住旧的C规则仍然有效(如每个程序员都应该知道的97件事):函数/方法应该只有几行长,真的!

I understand it is a very basic concept in the oops. But still I cannot get my head around. I understood why member variables are private, so class user cannot abuse it by setting up invalid values.

But how can this apply to the methods ?

解决方案

Lot of good answers, but maybe one more from a self-taught Java programmer as I went through all that by myself with a lot of pain ;)

Think about a Class as something seen from the outside, not as something you see internally. If you look at a Class from the outside, what you see?

Taking the clock as an example again, a clock can give you info about the current time and it can be set up to show the right time.

So looking at things from the outside, a clock is a machine that can do those two things; public methods we call them.

But we as constructors of this clock we know that before any time operation we have to switch from 23 to 11 on our display (it's that kind of clock), so we have to rearrange things internally a bit to do so. Changing from 23 to 11 works just fine for us in both cases - setting the clock and showing the current time - but we do it "on the side" as the user doesn't have to know about all that complicated math. These are private methods!

So our Clock Class could have two public methods (showTime and setTime) which are all that the user wants to see, and a private method (recountTime) that provides functionality for these public methods and are something that the user doesn't want to see.

So on the one hand, you should keep in mind that private is what won't be reimplemented and accessed by future programmers using your code (as was pointed at in the answers above). But private also means things done on the side, so the user don't see it. That's why we call the public methods a public interface - it's all the user will see from the outside.

For me it is helpful (I'm self-taught, so maybe it's not a very popular methodology...) to write down everything the users (real users and other Classes) will do with my Class (public interface with just public methods' signatures), then to write the signatures of private methods that I-the-implementer will use to accomplish the public goals that promised to provide to my users and then just fulfill it with code.

It can be helpful to keep in mind that the old C rule is still valid (as was expressed in 97 Things Every Programmer Should Know): a function/method should be just a few lines long, really!!

这篇关于为什么要“私有"?面向对象的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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