Java类(多项1对多映射)

  1 class Province {
  2     private int pid;
  3     private String name;
  4     private Stata stata;
  5     private City citys [] ;
  6     public void setStata(Stata stata){
  7       this.stata=stata;
  8       }
  9     public Stata getStata(){
 10       return this.stata;
 11       }
 12     public void setCitys(City citys[]){
 13       this.citys=citys;
 14       }
 15     public City [] getCity(){
 16       return this.citys;
 17       }
 18     public Province (int pid,String name){
 19       this.pid=pid;
 20       this.name=name;
 21       }
 22     public String getInfo(){
 23      return "省份ID:"+pid+" , 名称:"+name;
 24      }
 25 }
 26 class City{
 27     private int cid;
 28     private String cname;
 29     private Province province;
 30     private Stata stata;
 31     public void setStata(Stata stata){
 32       this.stata=stata;
 33       }
 34     public Stata getStata(){
 35       return this.stata;
 36       }
 37     public void setProvince(Province province){
 38        this.province=province;
 39        }
 40     public Province getProvince(){
 41        return this.province;
 42        }
 43     public City(int cid,String cname){
 44     this.cid=cid;
 45     this.cname=cname;
 46     }
 47     public String getInfo(){
 48     return "城市ID:"+cid+",城市名称:"+cname;
 49     }
 50 }
 51 class  Stata {
 52     private int id;
 53     private String sname;
 54     private Province prs [] ;
 55     private City citys [];
 56     public void setCitys(City citys [] ){
 57        this.citys=citys;
 58        }
 59     public City [] getCitys(){
 60        return this.citys;
 61        }
 62     public void setPrs(Province prs [] ){
 63       this.prs=prs;
 64       }
 65     public Province [] getPrs(){
 66       return this.prs;
 67       }
 68     public Stata(int id,String sname){
 69      this.id=id;
 70      this.sname=sname;
 71      }
 72    public String getInfo(){
 73     return "国家ID:"+id+",国家名称:"+sname;
 74     }
 75 }
 76 public class dare
 77 {public static void main(String args[]){
 78    City cd = new City(7,"南昌");
 79    City cc = new City(6,"宜春");
 80    City cb = new City(5,"广州");
 81    City ca = new City(4,"深圳");
 82    Province pb = new Province(3,"江西");
 83    Province pa = new Province(2,"广东");
 84    Stata st = new Stata(1,"中国");
 85    pa.setStata(st);
 86    pb.setStata(st);
 87    st.setPrs(new Province [] {pa,pb});
 88    cd.setProvince(pb);
 89    cc.setProvince(pb);
 90    cb.setProvince(pa);
 91    ca.setProvince(pa);
 92    pb.setCitys(new City[] {cd,cc});
 93    pa.setCitys(new City[] {cb,ca});
 94    cd.setStata(st);
 95    cc.setStata(st);
 96    cb.setStata(st);
 97    ca.setStata(st);
 98    st.setCitys(new City[] {cd,cc,cb,ca});
 99    System.out.println(ca.getStata().getInfo());
100    System.out.println("***************开始取出数据****************");//通过国家查找全部对应省份信息
101    for (int a =0;a<st.getPrs().length ;a++ ){
102        System.out.println(st.getPrs()[a].getInfo());
103    }
104     System.out.println("***************开始取出数据****************");//通过国家查找全部城市信息 以及全部对应省份信息
105    for (int b =0;b<st.getCitys().length ;b++ ){
106        System.out.println(st.getCitys()[b].getInfo());
107        System.out.println("\t|-"+st.getCitys()[b].getProvince().getInfo());
108    }
109   System.out.println("***************开始取出数据****************");//通过省份查找对应城市信息
110    System.out.println(pa.getInfo());
111    for (int x =0;x<pa.getCity().length ;x++ )
112    {System.out.println(pa.getCity() [x].getInfo());
113    }
114    }
115 }

 

posted @ 2016-09-15 17:13  as145  阅读(922)  评论(0编辑  收藏  举报