1 classProvince {2 Private intpid;3 PrivateString name;4 PrivateStata Stata;5 PrivateCity Citys [];6 Public voidSetstata (Stata Stata) {7 This. stata=Stata;8 }9 PublicStata Getstata () {Ten return This. Stata; One } A Public voidSetcitys (city citys[]) { - This. citys=Citys; - } the PublicCity [] getcity () { - return This. Citys; - } - PublicProvince (intpid,string name) { + This. pid=pid; - This. name=name; + } A PublicString GetInfo () { at return"Province ID:" +pid+ ", Name:" +name; - } - } - classcity{ - Private intCID; - PrivateString cname; in PrivateProvince Province; - PrivateStata Stata; to Public voidSetstata (Stata Stata) { + This. stata=Stata; - } the PublicStata Getstata () { * return This. Stata; $ }Panax Notoginseng Public voidSetprovince (province province) { - This. province=Province; the } + PublicProvince Getprovince () { A return This. Province; the } + PublicCity (intcid,string CNAME) { - This. cid=CID; $ This. cname=cname; $ } - PublicString GetInfo () { - return"City ID:" +cid+ ", City Name:" +cname; the } - }Wuyi classStata { the Private intID; - PrivateString sname; Wu Privateprovince PRs []; - PrivateCity Citys []; About Public voidSetcitys (city Citys []) { $ This. citys=Citys; - } - PublicCity [] Getcitys () { - return This. Citys; A } + Public voidSetprs (province PRs []) { the This. prs=PRs; - } $ PublicProvince [] Getprs () { the return This. PRs; the } the PublicStata (intid,string sname) { the This. id=ID; - This. sname=sname; in } the PublicString GetInfo () { the return"Country ID:" +id+ ", Country name:" +sname; About } the } the Public classDare the{ Public Static voidMain (String args[]) { +City CD =NewCity (7, "Nanchang"); -City cc =NewCity (6, "Yichun"); theCity cb =NewCity (5, "Guangzhou");BayiCity CA =NewCity (4, "Shenzhen"); theProvince PB =NewProvince (3, "Jiangxi"); theProvince PA =NewProvince (2, "Guangdong"); -Stata st =NewStata (1, "China"); - Pa.setstata (ST); the Pb.setstata (ST); theSt.setprs (NewProvince [] {PA,PB}); the cd.setprovince (pb); the cc.setprovince (pb); - Cb.setprovince (PA); the Ca.setprovince (PA); thePb.setcitys (Newcity[] {cd,cc}); thePa.setcitys (Newcity[] {cb,ca});94 Cd.setstata (ST); the Cc.setstata (ST); the Cb.setstata (ST); the Ca.setstata (ST);98St.setcitys (Newcity[] {cd,cc,cb,ca}); About System.out.println (Ca.getstata (). GetInfo ()); -System.out.println ("*************** begins to take out data ****************");//find all relevant provinces by country101 for(intA =0;a<st.getprs (). length; a++ ){102 System.out.println (St.getprs () [A].getinfo ());103 }104System.out.println ("*************** begins to take out data ****************");//Find all city information and all corresponding province information through the country the for(intb =0;b<st.getcitys (). length; b++ ){106 System.out.println (St.getcitys () [B].getinfo ());107System.out.println ("\t|-" +st.getcitys () [B].getprovince (). GetInfo ());108 }109System.out.println ("*************** begins to take out data ****************");//Find city information by province the System.out.println (Pa.getinfo ());111 for(int× =0;x<pa.getcity (). length; x + + ) the {System.out.println (pa.getcity () [X].getinfo ()];113 } the } the}
Java Class (multiple 1-to-many mappings)