Java类(1对多)

 1 class Province {
 2     private int pid;
 3     private String name;
 4     private City citys [] ;
 5     public void setCitys(City citys[]){
 6       this.citys=citys;
 7       }
 8     public City [] getCity(){
 9       return this.citys;
10       }
11     public Province (int pid,String name){
12       this.pid=pid;
13       this.name=name;
14       }
15     public String getInfo(){
16      return "省份ID:"+pid+" , 名称:"+name;
17      }
18 }
19 class City{
20     private int cid;
21     private String cname;
22     private Province province;
23     public void setProvince(Province province){
24        this.province=province;
25        }
26     public Province getProvince(){
27        return this.province;
28        }
29     public City(int cid,String cname){
30     this.cid=cid;
31     this.cname=cname;
32     }
33     public String getInfo(){
34     return "城市ID:"+cid+",城市名称:"+cname;
35     }
36 }
37 
38 public class dare
39 {public static void main(String args[]){
40    City aa = new City(1,"江西");
41    City bb = new City(2,"湖南");
42    Province pr = new Province(0,"中国");
43    aa.setProvince(pr);
44    bb.setProvince(pr);
45    pr.setCitys(new City[] {aa,bb});
46    System.out.println("*****************开始取出省份信息****************");
47    System.out.println(aa.getProvince().getInfo());
48    System.out.println("*****************开始取出城市信息****************");
49    System.out.println(pr.getInfo());
50    for (int x = 0;x<pr.getCity().length ;x++ )
51    {System.out.println("\t|-"+pr.getCity()[x].getInfo() );
52    }
53   System.out.println("*****************已经全部取出****************");
54    }
55 }
View Co
 1 class Province {
 2     private int pid;
 3     private String name;
 4     private City citys [] ;
 5     public void setCitys(City citys[]){
 6       this.citys=citys;
 7       }
 8     public City [] getCity(){
 9       return this.citys;
10       }
11     public Province (int pid,String name){
12       this.pid=pid;
13       this.name=name;
14       }
15     public String getInfo(){
16      return "省份ID:"+pid+" , 名称:"+name;
17      }
18 }
19 class City{
20     private int cid;
21     private String cname;
22     private Province province;
23     public void setProvince(Province province){
24        this.province=province;
25        }
26     public Province getProvince(){
27        return this.province;
28        }
29     public City(int cid,String cname){
30     this.cid=cid;
31     this.cname=cname;
32     }
33     public String getInfo(){
34     return "城市ID:"+cid+",城市名称:"+cname;
35     }
36 }
37 
38 public class dare
39 {public static void main(String args[]){
40    City aa = new City(1,"江西");
41    City bb = new City(2,"湖南");
42    Province pr = new Province(0,"中国");
43    aa.setProvince(pr);
44    bb.setProvince(pr);
45    pr.setCitys(new City[] {aa,bb});
46    System.out.println("*****************开始取出省份信息****************");
47    System.out.println(aa.getProvince().getInfo());
48    System.out.println("*****************开始取出城市信息****************");
49    System.out.println(pr.getInfo());
50    for (int x = 0;x<pr.getCity().length ;x++ )
51    {System.out.println("\t|-"+pr.getCity()[x].getInfo() );
52    }
53   System.out.println("*****************已经全部取出****************");
54    }
55 }

 

de

 

posted @ 2016-09-15 10:50  as145  阅读(176)  评论(0编辑  收藏  举报