-2

我打算有一个方法来创建一个新对象,这样我每次调用它时都可以更新该方法

这是代码:

public class CollectedData {
     List<BusSearchOuterDataResults> busData = new ArrayList<BusSearchOuterDataResults>();
     public BusSearchOuterDataResults outerData;
     public void createNewOuterDataObject(){
         outerData = new BusSearchOuterDataResults();
         this.outerData = outerData;
     }
     public void createNewInnerDataObject(){
         outerData.createNewInnerDataObject();
     }
     public void addAllCollectedData(){
         busData.add(outerData);
     }
     public List<BusSearchOuterDataResults> getBusData(){
         return busData;
     }
}

为什么当我调用 createNewOuterDataObject 方法时它不会让我创建一个新方法,是因为我已经调用了 createNewOuterDataObject 的类 obj 而没有任何初始化?

public class BusSearchOuterDataResults {
List<BusSearchInnerDataResults> innerData = new ArrayList<BusSearchInnerDataResults>();

private String routeNo;
private String routeLabel;
private String direction;
public BusSearchInnerDataResults innerDataOBJ = new BusSearchInnerDataResults();

public BusSearchOuterDataResults(String routeNo, String routeLabel, String direction){
    this.routeNo = routeNo;
    this.routeLabel = routeLabel;
    this.direction = direction;
}

public BusSearchOuterDataResults(){

}

public void createNewInnerDataObject(){
    BusSearchInnerDataResults innerDataOBJ = new BusSearchInnerDataResults();
    this.innerDataOBJ = innerDataOBJ;
}
public void addAllCollectedData(){
    innerData.add(new BusSearchInnerDataResults());
}

public void setRouteNo(String routeNo){
    this.routeNo = routeNo;
}
public void setRouteLabel(String routeLabel){
    this.routeLabel = routeLabel;
}
public void setDirection(String direction){
    this.direction = direction;
}
public String getrouteNo(){
    return routeNo;
}
public String getrouteLabel(){
    return routeLabel;
}
public String getdirection(){
    return direction;
}
public List<BusSearchInnerDataResults> getData(){
    return innerData;
}
}
4

1 回答 1

1

您的代码不正确:

public void createNewOuterDataObject(){
   outerData = new BusSearchOuterDataResults(); // here you are initializing your field
   this.outerData = outerData; // but here ? what is this line for ? 
}

第二行什么都不做,就像做

int x = 0 ;
 x=x;
于 2013-09-30T19:48:06.150 回答