求助:关于对象参数传递的问题
我在写一个程序,测试过程中发现对象的值传递有错误,由于我对对象的理解不是很深刻,一直没有有效的改正错误,希望有高人能指点一二。现在我写了一个测试用的程序,一共由两个类组成Company和ReaderTest,其中main函数在ReaderTest中,源代码如下:
/********************Company类开始***********************************/
public class Company {
public String name;
public Company[] next=new Company[5];//
public Company[] previous=new Company[5];//im Feld gespeichert
public Company(){//constructor without parameter
name="";
for(int i=0;i<this.next.length;i++){
this.next[i]=null;
this.previous[i]=null;
}
}
public Company(String name){
this.name=name;
for(int i=0;i<this.next.length;i++){
this.next[i]=new Company();
this.previous[i]=new Company();
}
}
public Company(String name, Company next, Company previous){
this.name=name;
this.next[0]=next;
this.previous[0]=previous;
for(int i=1;i<this.next.length;i++){
this.next[i]=new Company();
this.previous[i]=new Company();
}
} //constructor with parameter
public Company[] getNext(){
return next;
}
public void setNext(Company[] c){
this.next=c;
}
public void addNext(Company c){
int add=0;
for(int i=0;i<5;i++){
if(this.next[i].getName().compareTo("")==1)
//if((this.next[i].getName()).compareTo(new String())!=1)
//if(this.next[i].equals(null))
add=i;
}
this.next[add]=c;
}
/*public void delNext(){
not necessary for now!
}*/
public Company[] getPrevious(){
return previous;
}
public void setPrevious(Company[] c){
this.previous=c;
}
public void addPrevious(Company c){
int add=0;
for(int i=0;i<5;i++){
if(this.previous[i]==null)
add=i;
}
this.previous[add]=c;
}
// public void delPrevious(){}
public String getName(){
return this.name;
}
public void setName(String s){
this.name=s;
}
//this method return the number of the hinder companies
public int howManyNext(){
int a = 0;
//String empty=" ";
for(int r=0;r<this.next.length;r++){
//if(this.next[r]!=null)
if(!this.next[r].getName().equals(" "))
a=r;}
return a;
}
public void setDuplicate(Company c){
this.setName(c.getName());
this.setNext(c.getNext());
this.setPrevious(c.getPrevious());
}
}
/******************************company类结束************************/
/**************************ReaderTest类开始*************************/
public class ReaderTest {
public Company c1;
/** Creates a new instance of ReaderTest */
public ReaderTest() {
c1=new Company("Company1");
}
public static void main(String[] args){
Company c2=new Company();
Company c3=new Company("Company2");
Company c4=new Company("COMPANY4");
rt.c1.addNext(c3);
//c2=rt.c1;
c2.setDuplicate(rt.c1);
c2.addNext(c4);
System.out.println("the rt.c1 has the name: "+rt.c1.getName());
System.out.println("the c2 has the name: "+c2.getName());
System.out.println("rt.c1 has next company "+rt.c1.howManyNext()+".And its name is"+rt.c1.next[0].getName());
System.out.println("c2 has next company "+c2.howManyNext()+".And its name is"+c2.next[0].getName());
}
}
/***********************************ReaderTest类结束****************************/
问题1:在ReaderTest的构造函数里我生成了一个Company c1,并且该Company有个string name属性为Company1,然后在后面我在c1的next数组中加入了另一个company c3;接下来我通过company类中的setDuplicate函数将c1的属性赋给了另一个company c2(这里c2的name是空字符串),并且在c2的next数组中我加入了第四个company c4 (c4的name属性值是COMPANY3).然后通过下面的System.out输出,然后可以看到c2的name属性被改为了Company1,这是我想得到的,但是在最后输出c1.next[0](这里应当是company2,也就是c3的name属性才对)时,却变成了"COMPANY3",也就是c4的属性值.请问,我该怎么改,才能在输出c1.next[0]时正确输出呢?