简单基础的问题,但是非常容易出错.
大家分析分析,为什么会出现这种情况:
一个Airport类,包括机场名和对应的航班.
1
2
public 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
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
调用此类的代码为:
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
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
你运行时会发现mAirport[0]和mAirport[1]的getAirlines()的结果是一样的,也就是说mAirport[0]这个Airport类的mAirlines[10]和mAirport[1]的一样,为什么?