我正在使用 JSON。所以我有以下 POJO 類Position,Person. 當Person我需要的類。我只需要接收字段的格式化值Person(我只使用這個類,Position它是我的 JSON 結構的類)在哪里更好地在Positionor 中實現格式化邏輯Person?第一個變體,格式化邏輯 Position classpublic Position { private String value; public String getFormattedValue() { //Value formatting... return value;}public Person { private Position position; ..other fields public String getFormattedValue() { return position.getFormattedValue(); }}//using String neededFormattedValue = new Person().getFormattedValue();第二個變體,格式化邏輯 Person classpublic Position { private String value; public String getValue() { return value;}public Person { private Position position; ..other fields public String getFormattedValue() { String value = position.getValue() //Value formatting... return value; }}//using String neededFormattedValue = new Person().getFormattedValue();
2 回答
ABOUTYOU
TA貢獻1812條經驗 獲得超5個贊
我在 JSON、POJO 和 XML 之間轉換時遇到問題。
因此,似乎最好使用 get / set 的確切名稱,盡管是第一個字符 - 始終大寫。
例如。如果字段使用正確的格式,Android Studio 通常會建議方法(函數)名稱。在類包含的地方private String value;,當您開始輸入public get...AS 時,應該會為您提供一個列表。get 方法的正確語法是public getValue() ....
public class abc()
{
private String value;
private int some_id_of_a_record;
private String anotherString;
public String getValue()
{...}
public int getSome_id_of_a_record()
{...}
public String getAnotherString()
{...}
... }
添加回答
舉報
0/150
提交
取消
