class Item
{private int iid;
private String name;
private String note;
private Subitem subitem[];
private Product product[];
public Item(int iid,String name,String note){
this.iid=iid;
this.name=name;
this.note=note;
}
public Item(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setIid(int iid){
this.iid=iid;
}
public int getIid(){
return this.iid;
}
public void setNote(String note){
this.note=note;
}
public String getNote(){
return this.note;
}
public void setSubitem(Subitem subitem[])
{
this.subitem=subitem;
}
public Subitem[]getSubitem()
{
return this.subitem;
}
public void setProduct(Product product[])
{
this.product=product;
}
public Product[]getProduct()
{
return this.product;
}
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 product[];
public Subitem(int sid, String name, String note){
this.sid=sid;
this.name=name;
this.note=note;
}
public Subitem(){}
public void setItem(Item item)
{
this.item=item;
}
public Item getItem()
{
return this.item;
}
public void setProduct(Product product[])
{
this.product=product;
}
public Product[]getProduct()
{
return this.product;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setSid(int sid){
this.sid=sid;
}
public int getSid(){
return this.sid;
}
public void setNote(String note){
this.note=note;
}
public String getNote(){
return this.note;
}
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 Product(){}
public void setSubitem(Subitem subitem[])
{
this.subitem=subitem;
}
public Subitem[] getSubitem()
{
return this.subitem;
}
public void setItem(Item item)
{
this.item=item;
}
public Item getItem()
{
return this.item;
}
public void setPid(int pid)
{
this.pid=pid;
}
public int getPid()
{
return this.pid;
}
public void setName(String name)
{
this.name=name;
}
public String setName()
{
return this.name;
}
public void setPrice(double price){
this.price=price;
}
public double getPrice(){
return this.price;
}
public String getInfo(){
return "商品"+this.pid+this.name+this.price;
}
}
public class PCtestse
{
public static void main(String args[])
{
Item item=new Item(1,"厨房用具","1");
Subitem suba=new Subitem(1001,"刀具","2");
Subitem subb=new Subitem(1001,"刀具","2");
Subitem subc=new Subitem(1001,"刀具","2");
Product proa=new Product(1002,"小刀",20);
Product prob=new Product(1003,"小刀",20);
suba.setItem(item);
suba.setItem(item);
suba.setItem(item);
item.setSubitem(new Subitem[]{suba,subb,subc});
proa.setSubitem(suba);
proa.setItem(item);
prob.setSubitem(subb);
prob.setItem(item);
suba.setProduct(proa);
subb.setProduct(prob);
//通过一个类型找到它所对应的全部子类型
System.out.println(item.getInfo());
//通过一个类型找到它所对应的全部商品,以及每个商品对应的子类型
// for(int x=0,x<item.getsubitem().length,x++)
// {
//System.out.println(item.getProduct()[x].getInfo());
//System.out.println(item.getSubitem()[x].getInfo());
//通过一个子类型找到所有对应的全部商品
//for(int x=0,x<item.getsubitem().length,x++)
// {
// System.out.println(item.getSubitem()[x].getInfo());
//}
}
}
出错:
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 Item(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setIid(int iid){
this.iid=iid;
}
public int getIid(){
return this.iid;
}
public void setNote(String note){
this.note=note;
}
public String getNote(){
return this.note;
}
public void setSubitems(Subitem subitems[])
{
this.subitems=subitems;
}
public Subitem[]getSubitems()
{
return this.subitems;
}
public void setProducts(Product products[])
{
this.products=products;
}
public Product[]getProducts()
{
return this.products;
}
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 Subitem(){}
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 void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setSid(int sid){
this.sid=sid;
}
public int getSid(){
return this.sid;
}
public void setNote(String note){
this.note=note;
}
public String getNote(){
return this.note;
}
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 Product(){}
public void setSubitem(Subitem subitem)
{
this.subitem=subitem;
}
public Subitem getSubitem()
{
return this.subitem;
}
public void setItem(Item item)
{
this.item=item;
}
public Item getItem()
{
return this.item;
}
public void setPid(int pid)
{
this.pid=pid;
}
public int getPid()
{
return this.pid;
}
public void setName(String name)
{
this.name=name;
}
public String setName()
{
return this.name;
}
public void setPrice(double price){
this.price=price;
}
public double getPrice(){
return this.price;
}
public String getInfo(){
return "商品"+this.pid+this.name+this.price;
}
}
public class PCtestse
{
public static void main(String args[])
{
Item item=new Item(1,"厨房用具","1");
Subitem suba=new Subitem(1001,"刀具","2");
Subitem subb=new Subitem(1001,"刀具","2");
Subitem subc=new Subitem(1001,"刀具","2");
Product proa=new Product(1002,"小刀",20);
Product prob=new Product(1003,"小刀",20);
suba.setItem(item);
suba.setItem(item);
suba.setItem(item);
item.setSubitems(new Subitem[]{suba,subb,subc});
proa.setSubitem(suba);
proa.setItem(item);
prob.setSubitem(subb);
prob.setItem(item);
suba.setProducts(new Product[]{proa,prob});
subb.setProducts(new Product[]{proa,prob});
//通过一个类型找到它所对应的全部子类型
System.out.println(item.getInfo());
for(int x=0;x<item.getSubitems().length;x++)
{
System.out.println(item.getSubitems()[x].getInfo());
}
//通过一个类型找到它所对应的全部商品,以及每个商品对应的子类型
System.out.println(item.getInfo());
for(int x=0;x<item.getProducts().length;x++)
{
System.out.println(item.getProducts()[x].getInfo());
System.out.println(item.getProducts()[x].getSubitem().getInfo());
}
//通过一个子类型找到所有对应的全部商品
System.out.println(subb.getInfo());
for(int x=0;x<subb.getProducts().length;x++)
{
System.out.println(subb.getProducts()[x].getInfo());
}
}
}
原文地址:https://www.cnblogs.com/Syiren/p/8666604.html