在移动应用程序中使用 getter setter 是否更好?

发布于 2024-12-20 18:34:25 字数 48 浏览 3 评论 0原文

我需要知道在移动开发中使用 getter setter 或使用公共实例变量哪个更好

I need to know that what is better to use getter setter or use public instance variable in mobile development

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈下烟灰 2024-12-27 18:34:25

这取决于您的应用程序上下文。如果要封装对象的属性,通常最好使用 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).

天气好吗我好吗 2024-12-27 18:34:25

本文建议不要使用iternal em> getter 和 setter。

遵循常见的面向对象编程实践是合理的
并且在公共接口中有 getter 和 setter,但是在一个
类,您应该始终直接访问字段。

This article recommends against the use of iternal getters and setters.

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.

攒眉千度 2024-12-27 18:34:25

在我看来,在任何类型的应用程序中当然应该避免使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文