Java简单类——双向一对多映射

  1 class Item {     // 父栏目
  2     private int iid ;
  3     private String name ;
  4     private String note ;
  5     private Subitem subitems [] ; //一个父栏目下有多个子栏目
  6     private Product products [] ; //一个栏目下包含多个商品
  7     public Item(int iid,String name,String note) {
  8         this.iid = iid ;
  9         this.name = name ;
 10         this.note = note ;
 11     }
 12     public void setSubitems(Subitem subitems[]) {
 13         this.subitems = subitems ;
 14     }
 15     public void setProducts(Product[] products) {
 16         this.products = products ;
 17     }
 18     public Product[] getProducts() {
 19         return this.products ;
 20     }
 21     public Subitem [] getSubitems() {
 22         return this.subitems ;
 23     }
 24     public String getInfo() {
 25         return "栏目编号:" + this.iid + ",名称:" + this.name + ",描述:" + this.note ;
 26     }
 27 }
 28 class Subitem {  // 子栏目
 29     private int sid ;
 30     private String name ;
 31     private String note ;
 32     private Item item ; // 一个子栏目对应一个父栏目
 33     private Product products [] ; //一个子栏目下包含多个商品
 34     public Subitem(int sid,String name,String note) {
 35         this.sid = sid ;
 36         this.name = name ;
 37         this.note = note ;
 38     }
 39     public void setItem(Item item) {
 40         this.item = item ;
 41     }
 42     public Item getItem() {
 43         return this.item ;
 44     }
 45     public void setProducts(Product products[]) {
 46         this.products = products ;
 47     }
 48     public Product[] getProducts() {
 49         return this.products ;
 50     }
 51     public String getInfo() {
 52         return "子栏目编号:" + this.sid + ",名称:" + this.name + ",描述:" + this.note ;
 53     }
 54 }
 55 class Product {  // 商品
 56     private int pid ;
 57     private String name ;
 58     private double price ;
 59     private Item item ;
 60     private Subitem subitem ;
 61     public Product(int pid,String name,double price) {
 62         this.pid = pid ;
 63         this.name = name ;
 64         this.price = price ;
 65     }
 66     public void setItem(Item item) {
 67         this.item = item ;
 68     }
 69     public Item getItem() {
 70         return this.item ;
 71     }
 72     public void setSubitem(Subitem subitem) {
 73         this.subitem = subitem ;
 74     }
 75     public Subitem getSubitem() {
 76         return this.subitem ;
 77     }
 78     public String getInfo() {
 79         return "商品编号:" + this.pid + ",名称:" + this.name + ",价格:" + this.price ;
 80     }
 81 }
 82 public class TestISP {
 83     public static void main(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(new Subitem[]{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(new Product[] {proa,prob,proc}) ;
116         subb.setProducts(new Product[] {prod,proe}) ;
117         subc.setProducts(new Product[] {prof,prog}) ;
118         item.setProducts(new Product[] {proa,prob,proc,prod,proe,prof,prog}) ;
119         //取出数据
120         //1、通过一个类型找到全部的子类型
121         System.out.println(item.getInfo()) ;
122         for (int x = 0;x <item.getSubitems().length ;x ++ ){
123             System.out.println("\t|-" + item.getSubitems()[x].getInfo()) ;
124         }
125         System.out.println("--------------------------------------------------------------") ;
126         System.out.println(item.getInfo()) ;
127         for (int x = 0;x <item.getProducts().length ;x ++ ){
128             System.out.println("\t|-" + item.getProducts()[x].getInfo()) ;
129             System.out.println("\t|-" + item.getProducts()[x].getSubitem().getInfo()) ;
130         }            
131         System.out.println("--------------------------------------------------------------") ;
132         System.out.println(subb.getInfo()) ;
133         for (int x = 0;x <subb.getProducts().length ;x ++ ){
134             System.out.println("\t|-" + subb.getProducts()[x].getInfo()) ;
135         }
136     }
137 }

第一次写上百行的代码啊QAQ,中间报错了几次折腾了好久才发现是尼玛设定set和get方法时竟然不知道怎么的把public写成了private,晕死,以后就知道了。错误如下图

最终完成时运行结果如下

posted @ 2016-03-10 03:00  蒋立  阅读(992)  评论(0编辑  收藏  举报