在移动应用程序中使用 getter setter 是否更好?
我需要知道在移动开发中使用 getter setter 或使用公共实例变量哪个更好
I need to know that what is better to use getter setter or use public instance variable in mobile development
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您的应用程序上下文。如果要封装对象的属性,通常最好使用 getter 和 setter。但是,如果您只想存储常量或默认值,请使用实例变量(或字段)。
It depends on your application context. If you want to encapsulate properties of an object, it's often better to use getters and setters. However if you just want to store a constant or default value, use an instance variable (or field).
本文建议不要使用iternal em> getter 和 setter。
This article recommends against the use of iternal getters and setters.
在我看来,在任何类型的应用程序中当然应该避免使用 setter/getter 和公共变量,因为 setter 和 getter 可能会破坏对象的有效状态。如果您需要在应用程序中进行序列化,我建议使用 Memnto 设计模式。
In my opinion the using of the setters/getters and public variables of course should be avoided in any type of application because setters and getters may broke the valid state of the object. In case you need a serialization in your application I would like to suggest to use Memnto design pattern.