【问题标题】:Problem with getter and setter with variable name same as a function变量名与函数相同的getter和setter问题
【发布时间】:2019-03-31 13:07:54
【问题描述】:

我用 getter 和 setter 解析一个 JSON 文件(使用 Gson)。一切正常。但在我的 JSON 中,我有一个名为“switch”的变量。如果我像这样在我的班级中包含这个名字

私有字符串开关;

这不起作用,因为 switch 是 Java 的一个函数,但如果我想要我的解析工作,我必须准确地使用这个名称。

希望我能做到吗?

谢谢

对不起,我对 Java 很陌生,我还在学习。

【问题讨论】:

标签: java android


【解决方案1】:

可以使用 GSON 的 Field Naming Support

@SerializedName("switch")
private String Switch;
public String getSwitch ()
{
    return Switch;
}
public void setSwitch (String aSwitch)
{
    Switch= aSwitch;
}

【讨论】:

  • 是的,没问题,你也可以去@JsonProperty
猜你喜欢
  • 2019-01-12
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2012-06-23
  • 2012-06-12
  • 1970-01-01
相关资源
最近更新 更多