简体   繁体   中英

How to create child object in java (android)

I'm php developer and I'm new in Java. I just want to create a multi-level object in java and assign them values. Please have a look at the object.

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

}

}

First, you need to create an object by defining a class

example

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;
}

And to assign default value use constructors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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