使用私人关键字 [英] the use of private keyword

查看:110
本文介绍了使用私人关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手。我现在正在学习Java,有些东西我不太确定,那就是使用私有。为什么程序员将变量设置为私有,然后将write,getter和setter设置为访问它。无论如何,为什么不把所有东西都公之于众。

I am new to programming. I am learning Java now, there is something I am not really sure, that the use of private. Why programmer set the variable as private then write , getter and setter to access it. Why not put everything in public since we use it anyway.

public class BadOO {
    public int size;

    public int weight;
    ...
}

public class ExploitBadOO {
    public static void main (String [] args) {
        BadOO b = new BadOO();
        b.size = -5; // Legal but bad!!
    }
}

我发现了一些这样的代码,我看到了评论合法但不好。我不明白为什么,请解释一下。

I found some code like this, and i saw the comment legal but bad. I don't understand why, please explain me.

推荐答案

不仅仅是将变量公之于众的主要原因是,如果你确实将变量公之于众,你会稍后会产生更多麻烦。

The main reason to not just make the variable public in the first place is that if you did make it public, you would create more headaches later on.

例如,一位程序员围绕私有成员变量编写公共getter和setter。三个月后,他需要验证变量永远不会设置为null。他在setFoo(...)方法中添加了一个检查,然后检查所有设置变量的尝试是否将其设置为null。案例已经结束,并且付出了很少的努力。

For example, one programmer writes public getters and setters around a private member variable. Three months later, he needs to verify that the variable is never "set" to null. He adds in a check in the "setFoo(...)" method, and all attempts to set the variable will then be checked for "setting it to null". Case closed, and with little effort.

另一个程序员意识到,围绕私有成员变量放入公共getter和setter是违反封装精神的,他认为这是徒劳的。方法并决定只使成员变量公开。也许这会带来一点性能提升,或者程序员可能只是想按照它使用它来编写它。三个月后,他需要验证变量永远不会设置为null。他扫描对变量的每次访问,有效地搜索整个代码库,包括可能通过反射访问变量的所有代码。这包括所有已扩展其代码的第三方库,以及在编写代码后使用其代码的所有新编写的模块。然后,他修改所有调用以保证变量永远不会设置为null。案件永远不会关闭,因为他无法有效地找到对公开成员的所有访问,也无法访问所有第三方源代码。由于对新编写的模块缺乏了解,调查保证不完整。最后,他无法控制可能访问公共成员的未来代码,该代码可能包含将成员变量设置为null的行。

Another programmer realizes that putting in public getters and setters around a private member variable is violating the spirit of encapsulation, he sees the futility of the methods and decides to just make the member variable public. Perhaps this gains a bit of a performance boost, or perhaps the programmer just wants to "write it as it is used". Three months later, he needs to verify that the variable is never "set" to null. He scans every access to the variable, effectively searching through the entire code base, including all code that might be accessing the variable via reflection. This includes all 3rd party libraries which has extended his code, and all newly written modules which used his code after it was written. He then either modifies all calls to guarantee that the variable is never set to null. The case is never closed, because he can't effectively find all accesses to the exposed member, nor does he have access to all 3rd party source code. With imperfect knowledge of newly written modules, the survey is guaranteed to be incomplete. Finally he has no control over the future code which may access the public member, and that code may contain lines which set the member variable to null.

当然第二个程序员然后可以通过在变量周围放置get和set方法并使其成为私有来破坏所有现有代码,但是,嘿,他可以在三个月前完成这个并且为自己解释为什么他需要打破其他人的代码。

Of course the second programmer could then break all existing code by putting "get" and "set" methods around the variable and making it private, but hey, he could have done that three months earlier and saved himself the explanation of why he needed to break everyone else's code.

按照你的意愿调用它,但在私有成员变量周围设置公共获取和设置方法是多年来带来的防御性编程(即几十年的经验。

Call it what you will, but putting public "get" and "set" methods around a private member variable is defensive programming which has been brought about by many years (i.e. decades) of experience.

这篇关于使用私人关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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