当我有一个变量,如exampleVariable
,我应该使用getExampleVariable()
和setExampleVariable(Object value)
并使exampleVariable
私人而不是ExampleClass.exampleVariable
和ExampleClass.exampleVariable = exampleObject;
?何时使用getter和setter?
即使getter和setter除了设置变量之外,我通常都会使用getter和setter。
是的。封装决定了这个原理 – Dan
[Pick](http://stackoverflow.com/questions/565095/are-getters-and-setters-evil?rq = 1)[和](http://stackoverflow.com/questions/875033/getters-setters-in-java?rq=1)[选择](http://stackoverflow.com/questions/875033/getters- setters-in-java?rq = 1)[a](http://stackoverflow.com/questions/10407877/what-is-the-point-of-getters-and-setters?lq=1)[重复]( http://stackoverflow.com/questions/1461598/what-is-the-point-of-setters-and-getters-in-java?lq=1)[question](http://stackoverflow.com/questions/ 11883454 /必要的吸气剂方法?lq = 1)! – eboix
@丹虽然有人会将这个原理称为“信息隐藏”。 – Scooter