1 class Item { //父栏目
2 private intiid ;3 privateString name ;4 privateString note ;5 private Subitem subitems [] ; //一个父栏目下有多个子栏目
6 private Product products [] ; //一个栏目下包含多个商品
7 public Item(intiid,String name,String note) {8 this.iid =iid ;9 this.name =name ;10 this.note =note ;11 }12 public voidsetSubitems(Subitem subitems[]) {13 this.subitems =subitems ;14 }15 public voidsetProducts(Product[] products) {16 this.products =products ;17 }18 publicProduct[] getProducts() {19 return this.products ;20 }21 publicSubitem [] getSubitems() {22 return this.subitems ;23 }24 publicString getInfo() {25 return "栏目编号:" + this.iid + ",名称:" + this.name + ",描述:" + this.note ;26 }27 }28 class Subitem { //子栏目
29 private intsid ;30 privateString name ;31 privateString note ;32 private Item item ; //一个子栏目对应一个父栏目
33 private Product products [] ; //一个子栏目下包含多个商品
34 public Subitem(intsid,String name,String note) {35 this.sid =sid ;36 this.name =name ;37 this.note =note ;38 }39 public voidsetItem(Item item) {40 this.item =item ;41 }42 publicItem getItem() {43 return this.item ;44 }45 public voidsetProducts(Product products[]) {46 this.products =products ;47 }48 publicProduct[] getProducts() {49 return this.products ;50 }51 publicString getInfo() {52 return "子栏目编号:" + this.sid + ",名称:" + this.name + ",描述:" + this.note ;53 }54 }55 class Product { //商品
56 private intpid ;57 privateString name ;58 private doubleprice ;59 privateItem item ;60 privateSubitem subitem ;61 public Product(int pid,String name,doubleprice) {62 this.pid =pid ;63 this.name =name ;64 this.price =price ;65 }66 public voidsetItem(Item item) {67 this.item =item ;68 }69 publicItem getItem() {70 return this.item ;71 }72 public voidsetSubitem(Subitem subitem) {73 this.subitem =subitem ;74 }75 publicSubitem getSubitem() {76 return this.subitem ;77 }78 publicString getInfo() {79 return "商品编号:" + this.pid + ",名称:" + this.name + ",价格:" + this.price ;80 }81 }82 public classTestISP {83 public static voidmain(String args[]){84 //准备出单独的类对象
85 Item item = new Item(1,"文具","-") ;86 Subitem suba = new Subitem(1001,"笔","-") ;87 Subitem subb = new Subitem(1002,"尺","-") ;88 Subitem subc = new Subitem(1003,"刀","-") ;89 Product proa = new Product(90001,"中性笔",2.0) ;90 Product prob = new Product(90002,"钢笔",10.0) ;91 Product proc = new Product(90003,"毛笔",8.0) ;92 Product prod = new Product(90004,"直尺",2.0) ;93 Product proe = new Product(90005,"三角尺",1.5) ;94 Product prof = new Product(90006,"美工刀",5.0) ;95 Product prog = new Product(90007,"剪刀",6.0) ;96 //设置类之间的引用关系
97 suba.setItem(item) ;98 subb.setItem(item) ;99 subc.setItem(item) ;100 item.setSubitems(newSubitem[]{suba,subb,subc}) ;101 proa.setSubitem(suba) ;102 proa.setItem(item) ;103 prob.setSubitem(suba) ;104 prob.setItem(item) ;105 proc.setSubitem(suba) ;106 proc.setItem(item) ;107 prod.setSubitem(subb) ;108 prod.setItem(item) ;109 proe.setSubitem(subb) ;110 proe.setItem(item) ;111 prof.setSubitem(subc) ;112 prof.setItem(item) ;113 prog.setSubitem(subc) ;114 prog.setItem(item) ;115 suba.setProducts(newProduct[] {proa,prob,proc}) ;116 subb.setProducts(newProduct[] {prod,proe}) ;117 subc.setProducts(newProduct[] {prof,prog}) ;118 item.setProducts(newProduct[] {proa,prob,proc,prod,proe,prof,prog}) ;119 //取出数据120 //1、通过一个类型找到全部的子类型
121 System.out.println(item.getInfo()) ;122 for (int x = 0;x