大家分析分析,为什么会出现这种情况:
一个Airport类,包括机场名和对应的航班.
1
2public class Airport
3{
4 private String mAirportName;
5 private String[] mAirlines=new String[10];
6
7 private void Init()
8 {
9 for(int i=0;i<10;i++)
10 {
11 mAirlines[i]="";
12 }
13 }
14 public Airport(String airport,String[] airlines)
15 {
16 Init();
17 mAirportName=airport;
18 mAirlines=airlines;
19 }
20 public void serAirport(String value)
21 {
22 mAirportName=value;
23 }
24 public String getAirport()
25 {
26 return mAirportName;
27 }
28 public void setAirlines(String[] airlines)
29 {
30 mAirlines=airlines;
31 }
32 public String[] getAirlines()
33 {
34 return mAirlines;
35 }
36}
2public class Airport
3{
4 private String mAirportName;
5 private String[] mAirlines=new String[10];
6
7 private void Init()
8 {
9 for(int i=0;i<10;i++)
10 {
11 mAirlines[i]="";
12 }
13 }
14 public Airport(String airport,String[] airlines)
15 {
16 Init();
17 mAirportName=airport;
18 mAirlines=airlines;
19 }
20 public void serAirport(String value)
21 {
22 mAirportName=value;
23 }
24 public String getAirport()
25 {
26 return mAirportName;
27 }
28 public void setAirlines(String[] airlines)
29 {
30 mAirlines=airlines;
31 }
32 public String[] getAirlines()
33 {
34 return mAirlines;
35 }
36}
调用此类的代码为:
1 private static void loadAirportsAndAirlines()
2 {
3 String strAirport="LGA";
4 Airport[] mAirport=new Airport[2];
5 String[] strAirlines=new String[10];
6 for(int i=0;i<10;i++)
7 strAirlines[i]="";
8
9 strAirlines[0]="AER LINGUS";
10 strAirlines[1]="AIR ARUBU";
11 strAirlines[2]="AMERICAN WEST";
12 strAirlines[3]="AMERICAN INTL";
13 strAirlines[4]="SOUTHWEST";
14 strAirlines[5]="DELTA INTL";
15 strAirlines[6]="MARK AIR";
16 strAirlines[7]="SAS";
17 strAirlines[8]="US AIR";
18 strAirlines[9]="CHINA EAST";
19 mAirport[0]=new Airport(strAirport,strAirlines);
20 //strAirlines=new String[200];
21 // for(int i=0;i<200;i++)
22 // strAirlines[i]="";
23 strAirport="JFK Airport";
24 strAirlines[0]="BAER LINGUS";
25 strAirlines[1]="BAIR ARUBU";
26 strAirlines[2]="BAMERICAN WEST";
27 strAirlines[3]="BAMERICAN INTL";
28 strAirlines[4]="BSOUTHWEST";
29 strAirlines[5]="DELTA INTL";
30 strAirlines[6]="MARK AIR";
31 strAirlines[7]="SAS";
32 strAirlines[8]="US AIR";
33 strAirlines[9]="CHINA EAST";
34 mAirport[1]=new Airport(strAirport,strAirlines);
35 //为什么mAirport[0]的mAirport[0]和mAirport[1]的getAirlines()的结果是一样呢?
36 }
2 {
3 String strAirport="LGA";
4 Airport[] mAirport=new Airport[2];
5 String[] strAirlines=new String[10];
6 for(int i=0;i<10;i++)
7 strAirlines[i]="";
8
9 strAirlines[0]="AER LINGUS";
10 strAirlines[1]="AIR ARUBU";
11 strAirlines[2]="AMERICAN WEST";
12 strAirlines[3]="AMERICAN INTL";
13 strAirlines[4]="SOUTHWEST";
14 strAirlines[5]="DELTA INTL";
15 strAirlines[6]="MARK AIR";
16 strAirlines[7]="SAS";
17 strAirlines[8]="US AIR";
18 strAirlines[9]="CHINA EAST";
19 mAirport[0]=new Airport(strAirport,strAirlines);
20 //strAirlines=new String[200];
21 // for(int i=0;i<200;i++)
22 // strAirlines[i]="";
23 strAirport="JFK Airport";
24 strAirlines[0]="BAER LINGUS";
25 strAirlines[1]="BAIR ARUBU";
26 strAirlines[2]="BAMERICAN WEST";
27 strAirlines[3]="BAMERICAN INTL";
28 strAirlines[4]="BSOUTHWEST";
29 strAirlines[5]="DELTA INTL";
30 strAirlines[6]="MARK AIR";
31 strAirlines[7]="SAS";
32 strAirlines[8]="US AIR";
33 strAirlines[9]="CHINA EAST";
34 mAirport[1]=new Airport(strAirport,strAirlines);
35 //为什么mAirport[0]的mAirport[0]和mAirport[1]的getAirlines()的结果是一样呢?
36 }
你运行时会发现mAirport[0]和mAirport[1]的getAirlines()的结果是一样的,也就是说mAirport[0]这个Airport类的mAirlines[10]和mAirport[1]的一样,为什么?