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

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

问题描述

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

但这如何适用于方法?

解决方案

很多很好的答案,但也许还有一个来自自学的 Java 程序员,因为我自己经历了很多痛苦;)

将类看作是从外部看到的东西,而不是从内部看到的东西.如果你从外面看一个 Class,你看到了什么?

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

所以从外面看,时钟是一台可以做这两件事的机器;我们称之为公共方法.

但是我们作为这个时钟的构造者我们知道在任何时间操作之前我们必须在我们的显示器上从 23 切换到 11(它是那种时钟),所以我们必须在内部重新排列一些东西来做到这一点.在这两种情况下,从 23 更改为 11 对我们来说都很好 - 设置时钟并显示当前时间 - 但我们在旁边"这样做,因为用户不必了解所有信息那个复杂的数学.这些是私有方法!

所以我们的Clock 类可以有两个公共方法(showTimesetTime)用户想要查看,以及为这些公共方法提供功能的私有方法(recountTime)用户不想看到的.

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

对我来说,写下用户(真实用户和其他类)将使用我的类(公共接口与只是公共方法的签名),然后编写私有方法的签名,I-the-implementer 将使用该签名来实现承诺提供给我的用户的公共目标,然后只需用代码实现它.

记住旧的 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天全站免登陆