您如何决定定义变量“私有”?
2022-09-03 16:22:35
我参加了一次面试。面试官问我为什么需要私有变量。如果你通过定义一个变量私有来实现一些东西,你不能通过定义在java中定义的任何其他访问修饰符来实现同样的目标吗?
According to Java Specification Languages, A private class member or constructor is accessible only within the body of the top level class (7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses.
但是,如果我正在设计一个类,我该如何决定定义一个变量“private”?