【问题标题】:How to create child object in java (android)如何在java(android)中创建子对象
【发布时间】:2019-06-11 06:29:02
【问题描述】:

我是 php 开发人员,我是 Java 新手。我只想在 java 中创建一个多级对象并为其分配值。请看一下对象。

Object = {
0 : { 
name : abc,
description : xyz,
},
1  : {
   0 : {
       name : abc1,
       description : abc2
   }
   1 : {
       name : abc 
       description add,
   }

}

}

【问题讨论】:

  • 创建一个 class 有这样两个变量 'name & description',但是为了保存你的 object 你需要从 JSON 格式 或任何其他解析方法 & 将通过使用 wrapper to your class 为您提供此类结果。

标签: java android arrays object child-objects


【解决方案1】:

首先,你需要通过定义一个类来创建一个对象

例子

class XYZ {
    //constructor to initialize value
    XYZ(X detail, Y information){
     this.detail = detail;
     this.information = information;
   }
    X detail;
    Y information
}

class X {
    //constructor to initialize value
   X(String name, String description){
   this.name = name
   this.information = information
  }
 String name;
 String description;
}

class Y {
 //constructor to initialize value
 Y(X first, X second){
     this.first = first;
     this.second = second;
   }
 X first;
 X second;
}

并使用构造函数分配默认值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多