[英]Why use private modifier for immutable final instance var?
I'm writing a Java class representing some simple geometry. 我正在编写一个代表一些简单几何的Java类。
At the top-most abstract
class (which itself is package-private ) I have declared attributes that I need be accessed from subclasses in the same package. 在最顶层的
abstract
类(它本身就是包私有 )中,我声明了需要从同一个包中的子类访问的属性。
If I declare an attribute as final
in the AbstractClass
, 如果我在
AbstractClass
声明一个属性为final
,
final int foo;
I'll able to access it in the package directly, without any fuss of a getter method. 我可以直接在包中访问它,而不用任何getter方法。 However.
然而。 Doing according to "praxis" (or what I believe is the common style) would be:
根据“praxis”(或我认为是常见的风格)做的将是:
private final int foo;
which would of course require a non- private
getter. 这当然需要非
private
吸气剂。 The subclasses would have to refer to foo
(which is a very relevant and quintessential attribute) as if it were some external object: 子类必须引用
foo
(这是一个非常相关和典型的属性),好像它是一些外部对象:
this.getFoo();
It adds code and removes the direct way of accessing these members (ie foo
). 它添加了代码并删除了访问这些成员的直接方式(即
foo
)。
Are there any disadvantages of skipping the private
modifier, since they're anyway final and I'm not worried of exposing these attributes internally in the package? 跳过
private
修饰符是否有任何缺点,因为它们无论如何都是最终的,我不担心在包内部暴露这些属性?
I'm aware of OO-advocates claiming that getters/setters is a very natural way for objects to access their own attributes - but when does this make any non-cosmetic, non-[insert any JavaBeans-style thing], difference? 我知道OO倡导者声称getter / setter是对象访问他们自己的属性的一种非常自然的方式 - 但是什么时候这会使任何非整形,非[插入任何JavaBeans风格的东西],差异?
Consider an inner class, Coordinate
, which is so simple because it has two int
attributes - leaving all usage for class OuterClass
: 考虑一个内部类
Coordinate
,它非常简单,因为它有两个int
属性 - 留下类OuterClass
所有用法:
class OuterClass{
final static class Coordinate{
final int x, y;
Coordinate(int x, int y){
this.x = x;
this.y = y;
}
}
Coordinate coordinate;
}
For this inner class - why would I bother with the praxis of creating a getter? 对于这个内部阶级 - 为什么我要为创造一个吸气剂的实践而烦恼? A getter would introduce more code and force any class in the same package to call
coordinate.getX();
getter会引入更多代码并强制同一个包中的任何类调用
coordinate.getX();
instead of simply coordinate.x;
而不是简单的
coordinate.x;
. 。 Any overhead here?
这有任何开销吗? Notice the
final
modifier on the class Coordinate
. 注意
Coordinate
类的final
修饰符。
The advantage of getters is decoupling interface from implementation. getter的优点是将接口与实现分离。 Today your
getFoo
might do nothing but return foo
, but in the future you might want, for example, to remote the foo
member and return a computed result instead. 今天你的
getFoo
可能getFoo
返回foo
,但是将来你可能想要远程getFoo
foo
成员并返回计算结果。 A getter will allow you to do that without requiring a change at each call site. 获取者将允许您在不需要在每个呼叫站点进行更改的情况下执行此操作。
If you are ever going to access this value from JSF Page, it will expect getFoo rather than just foo (even if you write object.foo
). 如果你要从JSF页面访问这个值,它会期望getFoo而不仅仅是foo(即使你写了
object.foo
)。
Apart from that- today this field is final, long time from now it can change (I guess). 除此之外 - 今天这个领域是最终的,很长一段时间它可以改变(我猜)。 Even if the chance is very close to 0, I believe that there is no overkill in following good practices.
即使机会非常接近0,我相信在遵循良好做法方面也没有过度杀伤力。 Vast majority of the time you are making changes than writing code from scratch, so it is much better to safeguard yourself whenever possible (if all it takes is writing
private
and in case of eclipse about 4 clicks to generate the getter and setter automaticaly). 大多数时候你进行更改而不是从头开始编写代码,因此最好尽可能保护自己(如果只需编写
private
并且在eclipse的情况下大约需要4次点击以自动生成getter和setter)。
To extend Feldgendler's answer (with information which may be useful for someone needing the answer of this question --- I believe this is relevant because it is really a question about encapsulation): 扩展Feldgendler的答案(对于需要回答这个问题的人可能有用的信息 - 我相信这是相关的,因为它确实是一个关于封装的问题):
In effect of using private
modifiers you will have to create the "getter" (eg int getX(){ ... }
) to sustain access. 在使用
private
修饰符时,您必须创建“getter”(例如int getX(){ ... }
)以维持访问。 Which can be declared in an implemented interface
. 哪个可以在已实现的
interface
声明。 A Java interface
does not allow declaration of instance variables, such as the example of final int x;
一个Java
interface
不允许实例变量,如该示例的声明final int x;
--- or of any other variable lacking the static
modifier. ---或缺少
static
修饰符的任何其他变量。 The interface will act as a declaration of the behavior any implementing class will have. 该接口将充当任何实现类将具有的行为的声明。
If implemented like: 如果实施如下:
Coordinate implements ICoordinate { ... }
it could be useful in many scenarios: 它在许多场景中都很有用:
The use of an interface 使用界面
interface IDatabase
) with the methods void insert( ... ); Object get( ... );
void insert( ... ); Object get( ... );
方法实现一个接口(可能是名称interface IDatabase
void insert( ... ); Object get( ... );
void insert( ... ); Object get( ... );
and perhaps a few more -- then implement his temporary solution. interface IDatabase
. interface IDatabase
的数据库。 To swap to the new database he might only need to change *one line of code* (eg the constructor call)! Using private modifier 使用私有修饰符
private
modifier would imply that it is NOT to be accessed out of scope. private
修饰符意味着它不会超出范围。 Or equivalently -- only to be accessed within a certain scope. public
, "package-private" (no modifier implies package-private) and protected
. public
,“package-private”(没有修饰符暗示package-private)和protected
。 static final double LIGHT_SPEED
) there is reasonable reason to omit a getter and have it public
instead. static final double LIGHT_SPEED
),有合理的理由省略getter并将其public
。 This being just convention as it will be clear to what is a constant and what is an object with its own members. final
keyword and its impact on optimization if interested. final
关键字及其对优化的影响。 They can be used in methods as well as attributes.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.