If you are discussing getters & setters in ANDROID, check what android documentation tell:- Avoid Internal Getters/Setters On Android, this is a bad idea. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.
Lets put it this way, you don't feel any need of having getters/setters and you plan to make your properties non-private. It doesn't appear to be any problem with this approach. But you must ask few questions to yourself.
Are you having any property whose value should undergo some checking before assignment? (Need for a setter)
Do you have any mutable property which you don't want to expose as it is? (Need for a getter)
Now, if you think your few properties need getters/setters, but not all. Then I would say create getter/setter for all of them for the sake of consistency. :)
发布评论
评论(3)
如果您正在讨论 getters & ANDROID 中的 setter,请查看 android 文档的说明:-
避免内部 Getter/Setter< br>
在 Android 上,这是一个坏主意。虚拟方法调用是昂贵的,比实例字段查找昂贵得多。遵循常见的面向对象编程实践并在公共接口中使用 getter 和 setter 是合理的,但在类中您应该始终直接访问字段。
If you are discussing getters & setters in ANDROID, check what android documentation tell:-
Avoid Internal Getters/Setters
On Android, this is a bad idea. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.
如果所有私有变量都希望从外部访问,那么是的。
假设您有某些不会从外部使用的标志,那么不需要这些标志的 getter/setter。
另请参阅
if all the private variables are expected to be accessed from out side then yes.
Suppose you have certain flags those aren't going to be used from outside then no need of getters/setters for those.
Also See
这么说吧,您觉得不需要 getter/setter,并且您计划将您的属性设为非私有。这种方法似乎没有任何问题。但你必须问自己几个问题。
现在,如果您认为您的少数属性需要 getter/setter,但不是全部。然后我会说为了一致性,为所有这些创建 getter/setter。 :)
进一步参见,Effective Java 第二版 ,
命名 惯例Lets put it this way, you don't feel any need of having getters/setters and you plan to make your properties non-private. It doesn't appear to be any problem with this approach. But you must ask few questions to yourself.
Now, if you think your few properties need getters/setters, but not all. Then I would say create getter/setter for all of them for the sake of consistency. :)
Further see, Effective Java 2nd Edition,
namingconventions