为什么GSON使用字段而不是getter / setter? [英] Why does GSON use fields and not getters/setters?

查看:357
本文介绍了为什么GSON使用字段而不是getter / setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么GSON使用 ONLY 字段(私人,公共,受保护)?
有没有办法让GSON只使用getter和setter?

Why does GSON use ONLY fields(private,public,protected)? Is there a way to tell GSON to use only getters and setters?

推荐答案

一般来说,当你序列化/反序列化一个对象,你这样做是为了最终获得对象状态的完全副本;因此,您通常想要规避OO设计中通常需要的封装。如果你没有规避封装,那么在序列化之前可能无法最终得到反序列化后具有完全相同状态的对象。此外,请考虑您不想为特定属性提供setter的情况。如果你正在通过getter和setter工作,应该如何进行序列化/反序列化?

Generally speaking when you serialize/deserialize an object, you are doing so to end up with an exact copy of the state of the object; As such, you generally want to circumvent the encapsulation normally desired in an OO design. If you do not circumvent the encapsulation, it may not be possible to end up with an object that has the exact same state after deserialization as it had prior to serialization. Additionally, consider the case where you do not want to provide a setter for a particular property. How should serialization/deserialization act if you are working through the getters and setters?

这篇关于为什么GSON使用字段而不是getter / setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆