简体   繁体   English

Getter/Setter 与其他数据之间的区别 object Class

[英]Difference between Getter/Setter and this other Data object Class

I'm working on a mobile application with APIs endpoints returning JSON data and i do a lot of JSON parsing for displaying different types of data in my application.我正在开发一个带有返回 JSON 数据的 API 端点的移动应用程序,并且我做了很多 JSON 解析以在我的应用程序中显示不同类型的数据。

For a long time i was using Getter Setter with constructor in my java object classes to handle data but recently by colleague told me a bit different and easy approach for handling this exact data.很长一段时间以来,我在 java object 类中使用带有构造函数的 Getter Setter 来处理数据,但最近同事告诉我一种不同且简单的方法来处理这些确切的数据。 The two different dataObject classes are两个不同的 dataObject 类是

The way I was handling my data manipulation我处理数据操作的方式

public class MyObject {
String _string1, _string2;
List<String> _stringsList;

public MyObject(String _string1, String _string2, List<String> _stringsList) {
    this._string1 = _string1;
    this._string2 = _string2;
    this._stringsList = _stringsList;
}

public String getString1() {
    return _string1;
}

public String getString2() {
    return _string2;
}

public List<PostO> getStringList() {
    return _stringsList;
}

}

The way my colleague suggested我同事建议的方式

public class MyObject {
public String _string1, _string2;
public List<String> _stringsList;

public MyObject() { }

}

And the app works fine with both methods.该应用程序适用于这两种方法。 and the second way is short and easy to make edits.第二种方法简短易行。 But whats the difference between these two?但这两者有什么区别? Is there any technical shortcoming in the second method or something?第二种方法有什么技术缺陷吗? I really need to know before converting my classes to this new method.在将我的课程转换为这种新方法之前,我真的需要知道。

I started using GSON with second method, if it matters.如果重要的话,我开始用第二种方法使用 GSON。

From documentation :文档

Using fields vs getters to indicate Json elements使用字段与吸气剂来指示 Json 元素

Some Json libraries use the getters of a type to deduce the Json elements.一些 Json 库使用类型的 getter 来推断 Json 元素。 We chose to use all fields (up the inheritance hierarchy) that are not transient, static, or synthetic.我们选择使用非瞬态、static 或合成的所有字段(向上 inheritance 层次结构)。 We did this because not all classes are written with suitably named getters.我们这样做是因为并非所有类都是用适当命名的 getter 编写的。 Moreover, getXXX or isXXX might be semantic rather than indicating properties.此外,getXXX 或 isXXX 可能是语义而不是指示属性。

However, there are good arguments to support properties as well.但是,也有很好的 arguments 来支持属性。 We intend to enhance Gson in a latter version to support properties as an alternate mapping for indicating Json fields.我们打算在后一个版本中增强 Gson 以支持属性作为指示 Json 字段的替代映射。 For now, Gson is fields-based.目前,Gson 是基于字段的。

As you can read, Gson is fields-based and in your case there is no difference between fields in two versions of the same class.如您所见, Gson是基于字段的,在您的情况下,同一 class 的两个版本中的字段之间没有区别。

If you use MyObject only for deserialisation purpose and later you convert it to some business model you can stay with shorter version.如果您仅将MyObject用于反序列化目的,然后您将其转换为某些业务 model 您可以使用较短的版本。 But in case, MyObject is propagated over many layers it could introduce inconsistency with other classes which uses getters / setters .但是,如果MyObject在许多层上传播,它可能会与使用getters / setters的其他类引入不一致。

Maybe Lombok library would be the best to use for you and your colleague?也许Lombok图书馆最适合您和您的同事使用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM