实现多对多的映射
class Country{
private String cid ;
private String name ;
private Province provinces[] ;
private City cities[] ;
private CountyTown countyTowns[] ;
public Country(String cid,String name) {
this.cid = cid ;
this.name = name ;
}
public void setProvinces(Province provinces[]) {
this.provinces = provinces ;
}
public Province [] getProvinces() {
return this.provinces ;
}
public void setCities( City cities[] ) {
this.cities = cities ;
}
public City [] getCities() {
return this.cities ;
}
public void setCountyTonwns(CountyTown countyTowns[]) {
this.countyTowns = countyTowns ;
}
public CountyTown [] getCountyTowns() {
return this.countyTowns ;
}
public String getInfo() {
return "国家编号: " + this.cid + ",名称:" + this.name ;
}
}
class Province{
private String pid ;
private String name ;
private Country country ;
private City cities[] ;
private CountyTown countyTowns[] ;
public Province(String pid,String name) {
this.pid = pid;
this.name = name;
}
public void setCountry(Country country) {
this.country = country ;
}
public Country getCountry() {
return this.country ;
}
public void setCities( City cities[] ) {
this.cities = cities ;
}
public City [] getCities() {
return this.cities ;
}
public void setCountyTonwns(CountyTown countyTowns[]) {
this.countyTowns = countyTowns ;
}
public CountyTown [] getCountyTowns() {
return this.countyTowns ;
}
public String getInfo() {
return "省份编号: " + this.pid + ",名称 :" + this.name ;
}
}
class City{
private int cid ;
private String name ;
private Country country ;
private Province province ;
private CountyTown countyTowns[] ;
public Object getCountyTowns;
public City(int cid,String name) {
this.cid = cid;
this.name = name;
}
public void setCountry(Country country) {
this.country = country ;
}
public Country getCountry() {
return this.country ;
}
public void setProvince(Province province ) {
this.province = province ;
}
public Province getProvince() {
return this.province;
}
public void setCountyTonwns(CountyTown countyTowns[]) {
this.countyTowns = countyTowns ;
}
public CountyTown [] getCountyTowns() {
return this.countyTowns ;
}
public String getInfo() {
return "城市编号: " + this.cid + ",名称 :" + this.name;
}
}
class CountyTown{
private int cid;
private String name;
private Country country ;
private Province province ;
private City city ;
public CountyTown(int cid,String name) {
this.cid = cid;
this.name = name;
}
public void setCountry(Country country) {
this.country = country ;
}
public Country getCountry() {
return this.country ;
}
public void setProvince(Province province ) {
this.province = province ;
}
public Province getProvince() {
return this.province;
}
public void setCity(City city) {
this.city = city ;
}
public City getCity() {
return this.city ;
}
public String getInfo() {
return "县城编号: " + this.cid + ",名称: " + this.name ;
}
}
public class TestCPC {
public static void main(String[] args){
Country c = new Country("01","中国") ;
Province p1 = new Province("001","安徽省") ;
Province p2 = new Province("002","江苏省") ;
p1.setCountry(c);
p2.setCountry(c);
c.setProvinces(new Province[] {p1,p2});
City c1 = new City(1001 ,"宿州市") ;
City c2 = new City(1002 ,"合肥市") ;
City c3 = new City(1003 ,"芜湖市") ;
City c4 = new City(1004 ,"滁州市") ;
c1.setProvince(p1);
c2.setProvince(p1);
c3.setProvince(p1);
c4.setProvince(p1);
p1.setCities(new City[] {c1,c2,c3,c4});
c.setCities(new City[] {c1,c2,c3,c4});
CountyTown ct1 = new CountyTown(10001,"灵璧县") ;
CountyTown ct2 = new CountyTown(10002,"萧县") ;
CountyTown ct3 = new CountyTown(10003,"砀山县") ;
CountyTown ct4 = new CountyTown(10004,"泗县") ;
CountyTown ct5 = new CountyTown(10005,"埇桥区") ;
ct1.setCity(c1);
ct2.setCity(c1);
ct3.setCity(c1);
ct4.setCity(c1);
ct5.setCity(c1);
c1.setCountyTonwns(new CountyTown[] {ct1,ct2,ct3,ct4,ct5});
p1.setCountyTonwns(new CountyTown[] {ct1,ct2,ct3,ct4,ct5});
c.setCountyTonwns(new CountyTown[] {ct1,ct2,ct3,ct4,ct5});
p2.setCities(null);
// printCS(c);
//
// printSC(p1);
// printCX(c1);
// printCSCX(c);
//
// printCPC(p1);
printCS(count(c));
}
//根据国家找到国家对应得省份
public static void printCS(Country c) {
System.out.println("--" + c.getInfo());
if(c.getProvinces() != null) {
for(int x = 0 ;x < c.getProvinces().length;x ++) {
System.out.println("\t--" + c.getProvinces()[x].getInfo());
}
}
}
//根据省份找到对应得城市输出
public static void printSC(Province p) {
System.out.println("--" + p.getInfo());
if(p.getCities() != null) {
for(int x = 0 ;x < p.getCities().length;x ++) {
System.out.println("\t--" + p.getCities()[x].getInfo());
}
}
}
//根据城市找到对应得县城输出
public static void printCX(City c) {
System.out.println("--" + c.getInfo());
if(c.getCountyTowns() != null) {
for(int x = 0 ;x < c.getCountyTowns().length ;x ++) {
System.out.println("\t--" + c.getCountyTowns()[x].getInfo());
}
}
}
//根据省份找到对应得城市县城输出
public static void printCPC(Province p) {
System.out.println("--" + p.getInfo());
if(p.getCities() != null) {
for(int x = 0 ;x < p.getCities().length ;x ++) {
System.out.println("\t--" + p.getCities()[x].getInfo());
if(p.getCities()[x].getCountyTowns() != null) {
for(int y = 0 ;y < p.getCities()[x].getCountyTowns().length ; y ++) {
System.out.println("\t\t--" + p.getCities()[x].getCountyTowns()[y].getInfo());
}
}
}
}
}
//根据国家找到对应得省份城市县城输出
public static void printCSCX(Country c) {
System.out.println("--" + c.getInfo());
if(c.getProvinces() != null) {
for(int x = 0 ;x< c.getProvinces().length ;x ++) {
System.out.println("\t--" + c.getProvinces()[x].getInfo());
if(c.getProvinces()[x].getCities() != null) {
for(int y = 0 ;y < c.getProvinces()[x].getCities().length ;y ++) {
System.out.println("\t\t--" +c.getProvinces()[x].getCities()[y].getInfo());
if(c.getProvinces()[x].getCities()[y].getCountyTowns != null) {
for(int z = 0 ;z < c.getProvinces()[x].getCities()[y].getCountyTowns().length ;z ++) {
System.out.println("\t\t\t--" + c.getProvinces()[x].getCities()[y].getCountyTowns()[z].getInfo());
}
}
}
}
}
}
}
}
测试程序匹配关系太复杂太繁琐了,有哪位大神可以告诉我怎么处理一下