1 class Province {
2 private int pid ;
3 private String name ;
4 private City cities [] ;
5 public Province (int pid,String name) {
6 this.pid = pid ;
7 this.name = name ;
8 }
9 public void setCities(City cities[] ){
10 this.cities = cities ;
11 }
12 public City[] getCities() {
13 return this.cities ;
14 }
15 public String getInfo() {
16 return "省份编号:" + this.pid + ",名称:" + this.name ;
17 }
18 }
19 class City {
20 private int cid ;
21 private String name ;
22 private Province province ;
23 public City (int cid,String name) {
24 this.cid = cid ;
25 this.name = name ;
26 }
27 public void setProvince(Province province){
28 this.province = province ;
29 }
30 public Province getProvince() {
31 return this.province ;
32 }
33 public String getInfo() {
34 return "城市编号:" + this.cid + ",名称:" + this.name ;
35 }
36 }
37 public class TestPC{
38 public static void main(String args[]){
39 Province pro = new Province (1,"湖北");
40 City c1 = new City (1001,"武汉") ;
41 City c2 = new City (1002,"孝感") ;
42 City c3 = new City (1003,"宜昌") ;
43 City c4 = new City (1004,"黄石") ;
44 c1.setProvince(pro) ;
45 c2.setProvince(pro) ;
46 c3.setProvince(pro) ;
47 c4.setProvince(pro) ;
48 pro.setCities(new City [] {c1,c2,c3,c4}) ; //一个省份有多个城市
49 System.out.println(c2.getProvince().getInfo()) ;
50 for (int x = 0 ;x < pro.getCities().length ;x ++ ){
51 System.out.println("\t|-" + pro.getCities()[x].getInfo()) ;
52 }
53 }
54 }
![](https://images2015.cnblogs.com/blog/902355/201603/902355-20160309233132835-587993514.jpg)