例如:給定一個分類表和子分類表java
獲得以下信息:c++
1.一個分類的完整信息;編程
2.根據分類獲取其對應的子分類app
package Mapping_transformation; class item { private long id ; private String title ; private subitem subitems[] ; //一個分類有多個子分類 public item(long id , String title) { this.id = id ; this.title = title ; } public void setSubitems(subitem[] subitems) { this.subitems = subitems; } public subitem[] getSubitems() { return subitems; } public String getInfo() { return "【分類信息】id = "+this.id + " title = "+this.title ; } } class subitem { private long id ; private String title ; private item item ; public subitem(long id , String title) { this.id = id ; this.title = title ; } public void setItem(item item) { this.item = item ; } public item getItem() { return item ; } public String getInfo() { return "【子分類信息】id = "+this.id + " title = "+this.title ; } } public class One_to_many { public static void main(String[] args) { item item = new item(001,"編程書籍"); subitem subitems[] = new subitem[]{ new subitem(002,"java從入門到放棄"), new subitem(003,"c++從入門到放棄") }; item.setSubitems(subitems); //一個分類有多個子分類 for(int i = 0 ; i<subitems.length ; i++) { subitems[i].setItem(item); } System.out.println(item.getInfo()); for(int i = 0 ; i<item.getSubitems().length ; i++) { System.out.println(item.getSubitems()[i].getInfo()); } } }
resul:
【分類信息】id = 1 title = 編程書籍 【子分類信息】id = 2 title = java從入門到放棄 【子分類信息】id = 3 title = c++從入門到放棄