【问题标题】:How to set data in model class?如何在模型类中设置数据?
【发布时间】:2015-08-05 23:03:35
【问题描述】:

我想解析 JSON 并设置数据。我有这个模型

public class Model {

    public List<Test>test;

    public class Test implements Serializable{
        String b;

        public Test(String a){
            this.b=a;
        }
        public String getId() {
            return a;
        }

        public void setId(String a) {
            this.a = a;
        }
    }
} 

另一个类,我想在Test类中设置数据:

model.Test mm = new Model.Test("1");

但我收到错误“不是闭门课”。

【问题讨论】:

    标签: java android arrays json model


    【解决方案1】:

    你不能在 java 中这样做,因为 Test 不是 static。要创建Test 的新实例,您需要一个外部类的实例,即Model

    Model model = new Model();
    Test test = model.new Test("1");
    

    如果您希望能够在没有外部类实例的情况下创建实例,请将类Test 标记为static。然后你就可以这样使用它了:

    Model.Test test = new Model.Test("1");
    

    【讨论】:

      猜你喜欢
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 2017-05-30
      • 2011-08-08
      相关资源
      最近更新 更多