一对多映射
作者:互联网
class 一对多映射
{
public static void main(String[] args)
{
//第一步:根据结构设置获取数据
Item item = new Item(1L,"图书分类");
Subitem[] subitems = new Subitem[]{
new Subitem(10L,"小说"),
new Subitem(11L,"名著")
};
for(int x = 0;x <subitems.length;x++ ){
subitems[x].setItem(item);
}
item.setSubitems(subitems);
//第二步:根据要求获取数据
for (int x = 0;x < item.getSubitem().length ;x++ )
{
System.out.println("-\t"+item.getSubitem()[x].getInfo());
}
}
}
class Item
{
private long iid; //分类ID
private String title; //名称
private Subitem[] Subitems;//一个分类下面有多个子分类
public Item(long iid,String title){ //构造方法
this.iid = iid;
this.title = title;
}
public void setSubitems(Subitem[] Subitems){ //得到Subitems数据
this.Subitems = Subitems;
}
public Subitem[] getSubitem(){ //返回Subitem数据
return this.Subitems;
}
public String getInfo(){
return "【分类信息】"+"iid="+this.iid+"title"+this.title;
}
}
class Subitem
{
private long sid; //分类ID
private String title; //名称
private Item Item; //一个上级分类
public Subitem(long sid,String title){ //构造方法
this.sid = sid;
this.title = title;
}
public void setItem(Item Item){ //得到Subitems数据
this.Item = Item;
}
public Item getItem(){ //返回Subitem数据
return this.Item ;
}
public String getInfo(){
return "【子分类信息】"+"sid="+this.sid+"title="+this.title;
}
}
标签:Subitems,String,映射,title,Item,Subitem,一对,public 来源: https://blog.csdn.net/weixin_46245201/article/details/110006340