按照表的要求将表的结构转换为类结构,同时可以获取如下信息:
获取一个分类的完整信息;
可以根据分类获取其对应的所有子分类的信息。
范例:
class Item{
private long iid;
private String title;
private Subitem subitems []; //多个子分类
public Item(long iid,String title){
this.iid = iid;
this.title = title;
}
public void setSubitems(Subitem[] subitems){
this.subitems = subitems;
}
public Subitem[] getSubitems(){
return this.subitems;
}
// setter、getter、无参构造略
public String getInfo(){
return "【分类信息】iid = " + this.iid + "、title = " + this.title;
}
}
class Subitem{
private long sid;
private String title;
private Item item; //分类
public Subitem(long sid,String title){
this.sid = sid;
this.title = title;
}
// setter、getter、无参构造略
public String getInfo(){
return "【子分类信息】sid =" + this.sid + "、title = " + this.title;
}
public void setItem(Item item){
this.item = item;
}
public Item getItem(){
return this.item;
}
}
public class JavaDemo{
public static void main(String args[]){
//第一步:根据结构设置对象数据
Item item = new Item(1L,"图书");
Subitem subitems[] = new Subitem[]{
new Subitem(10L, "编程图书"),
new Subitem(11L, "图形图像类图书"),
new Subitem(12L, "文学图书"),
new Subitem(13L, "漫画图书"),
};
item.setSubitems(subitems);
for(int x = 0; x < subitems.length; x++){
subitems[x].setItem(item);
}
//第二步:根据要求获取数据
System.out.println(item.getInfo());
for(int x = 0; x < item.getSubitems().length; x++){
System.out.println("\t|- " + item.getSubitems()[x].getInfo());
}
}
}