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

时间:2023-03-08 17:44:06
Java简单类——双向一对多映射
 class Item {     // 父栏目
private int iid ;
private String name ;
private String note ;
private Subitem subitems [] ; //一个父栏目下有多个子栏目
private Product products [] ; //一个栏目下包含多个商品
public Item(int iid,String name,String note) {
this.iid = iid ;
this.name = name ;
this.note = note ;
}
public void setSubitems(Subitem subitems[]) {
this.subitems = subitems ;
}
public void setProducts(Product[] products) {
this.products = products ;
}
public Product[] getProducts() {
return this.products ;
}
public Subitem [] getSubitems() {
return this.subitems ;
}
public String getInfo() {
return "栏目编号:" + this.iid + ",名称:" + this.name + ",描述:" + this.note ;
}
}
class Subitem { // 子栏目
private int sid ;
private String name ;
private String note ;
private Item item ; // 一个子栏目对应一个父栏目
private Product products [] ; //一个子栏目下包含多个商品
public Subitem(int sid,String name,String note) {
this.sid = sid ;
this.name = name ;
this.note = note ;
}
public void setItem(Item item) {
this.item = item ;
}
public Item getItem() {
return this.item ;
}
public void setProducts(Product products[]) {
this.products = products ;
}
public Product[] getProducts() {
return this.products ;
}
public String getInfo() {
return "子栏目编号:" + this.sid + ",名称:" + this.name + ",描述:" + this.note ;
}
}
class Product { // 商品
private int pid ;
private String name ;
private double price ;
private Item item ;
private Subitem subitem ;
public Product(int pid,String name,double price) {
this.pid = pid ;
this.name = name ;
this.price = price ;
}
public void setItem(Item item) {
this.item = item ;
}
public Item getItem() {
return this.item ;
}
public void setSubitem(Subitem subitem) {
this.subitem = subitem ;
}
public Subitem getSubitem() {
return this.subitem ;
}
public String getInfo() {
return "商品编号:" + this.pid + ",名称:" + this.name + ",价格:" + this.price ;
}
}
public class TestISP {
public static void main(String args[]){
//准备出单独的类对象
Item item = new Item(1,"文具","-") ;
Subitem suba = new Subitem(1001,"笔","-") ;
Subitem subb = new Subitem(1002,"尺","-") ;
Subitem subc = new Subitem(1003,"刀","-") ;
Product proa = new Product(90001,"中性笔",2.0) ;
Product prob = new Product(90002,"钢笔",10.0) ;
Product proc = new Product(90003,"毛笔",8.0) ;
Product prod = new Product(90004,"直尺",2.0) ;
Product proe = new Product(90005,"三角尺",1.5) ;
Product prof = new Product(90006,"美工刀",5.0) ;
Product prog = new Product(90007,"剪刀",6.0) ;
//设置类之间的引用关系
suba.setItem(item) ;
subb.setItem(item) ;
subc.setItem(item) ;
item.setSubitems(new Subitem[]{suba,subb,subc}) ;
proa.setSubitem(suba) ;
proa.setItem(item) ;
prob.setSubitem(suba) ;
prob.setItem(item) ;
proc.setSubitem(suba) ;
proc.setItem(item) ;
prod.setSubitem(subb) ;
prod.setItem(item) ;
proe.setSubitem(subb) ;
proe.setItem(item) ;
prof.setSubitem(subc) ;
prof.setItem(item) ;
prog.setSubitem(subc) ;
prog.setItem(item) ;
suba.setProducts(new Product[] {proa,prob,proc}) ;
subb.setProducts(new Product[] {prod,proe}) ;
subc.setProducts(new Product[] {prof,prog}) ;
item.setProducts(new Product[] {proa,prob,proc,prod,proe,prof,prog}) ;
//取出数据
//1、通过一个类型找到全部的子类型
System.out.println(item.getInfo()) ;
for (int x = 0;x <item.getSubitems().length ;x ++ ){
System.out.println("\t|-" + item.getSubitems()[x].getInfo()) ;
}
System.out.println("--------------------------------------------------------------") ;
System.out.println(item.getInfo()) ;
for (int x = 0;x <item.getProducts().length ;x ++ ){
System.out.println("\t|-" + item.getProducts()[x].getInfo()) ;
System.out.println("\t|-" + item.getProducts()[x].getSubitem().getInfo()) ;
}
System.out.println("--------------------------------------------------------------") ;
System.out.println(subb.getInfo()) ;
for (int x = 0;x <subb.getProducts().length ;x ++ ){
System.out.println("\t|-" + subb.getProducts()[x].getInfo()) ;
}
}
}

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

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

最终完成时运行结果如下

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