Java 我应该把getter函数放在哪里?

Java 我应该把getter函数放在哪里?,java,getter-setter,Java,Getter Setter,下面的类是一个上下文生成器,它将树对象放置在网格上的地理空间中。我创建了一个具有各种适用性值和ID的trees对象数组列表: public class TreeBuilder implements ContextBuilder<Object> { @Override public Context build(Context<Object> context) { context.setId("taylor"); ContinuousSpaceFacto

下面的类是一个上下文生成器,它将树对象放置在网格上的地理空间中。我创建了一个具有各种适用性值和ID的trees对象数组列表:

public class TreeBuilder implements ContextBuilder<Object> {


@Override
public Context build(Context<Object> context) {
    context.setId("taylor");

    ContinuousSpaceFactory spaceFactory = 
    ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(null);
    ContinuousSpace<Object> space = 
    spaceFactory.createContinuousSpace("space", context, 
            new RandomCartesianAdder<Object>(), 
            new repast.simphony.space.continuous.WrapAroundBorders(), 
            50, 50);


    GridFactory gridFactory = GridFactoryFinder.createGridFactory(null);
    Grid<Object> grid = gridFactory.createGrid("grid", context, 
            new GridBuilderParameters<Object>(new WrapAroundBorders(), 
            new SimpleGridAdder<Object>(), 
            true, 50, 50));

    ArrayList<Tree> trees = new ArrayList<Tree>();

    int treeCount = 100;
    for (int i = 1; i < treeCount; i++) {
        double suitability = Math.random();
        int id = i;


    Tree tree = new Tree(space, grid, suitability, id);
    context.add(tree);
    trees.add(tree);



    tree.measureSuit();

    }

    Tree maxTree = Collections.max(trees, new SuitComp());
    System.out.println(maxTree);

    for (Object obj : context) {
        NdPoint pt = space.getLocation(obj);
        grid.moveTo(obj, (int)pt.getX(), (int)pt.getY());

    }


    return context; 

}


}   
公共类TreeBuilder实现ContextBuilder{
@凌驾
公共上下文构建(上下文){
setId(“泰勒”);
ContinuousSpaceFactory spaceFactory=
ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(空);
连续空间=
spaceFactory.createContinuousSpace(“空间”,上下文,
新的Cartesianadder(),
新建repast.simphony.space.continuous.WrapAroundBorders(),
50, 50);
GridFactory GridFactory=GridFactoryFinder.createGridFactory(null);
Grid Grid=gridFactory.createGrid(“网格”,上下文,
新的GridBuilderParameters(新的WrapAroundBorders(),
新的SimpleGridder(),
对,五十,五十),;
ArrayList树=新的ArrayList();
int treeCount=100;
对于(int i=1;i
我相信我可以使用getter访问其他类中的列表。像这样的

public ArrayList<Tree> getList() {
return trees;
}
public ArrayList getList(){
归还树木;
}
但我的问题是:我应该把上面的代码放在哪里?每当我放置它时,我都会得到一个错误,特别是“returntrees;”

此外:我还可以使用getter从列表中获取maxTree值吗?

不在此上下文中

通常使用getter访问字段<代码>树
在方法
build
中声明为局部变量。这意味着每次调用它时,您都会得到一个新列表,一旦您从该方法返回,它就不再存在

如果您真的想存储树列表(我不知道您为什么要存储),您必须将其移动到字段声明:

private List<Tree> trees = new ArrayList<>();
private List trees=new ArrayList();
maxTree
值也存在类似问题;如果您想要存储它,并且这看起来与您的实例保持一致是合理的,那么您也必须将其移动到字段中。它不像上面的声明那样简单,因为您只知道该方法中的值是什么,但是它的调用不应该比它复杂得多。我把它作为练习留给读者。

不是在本文中

通常使用getter访问字段<代码>树在方法
build
中声明为局部变量。这意味着每次调用它时,您都会得到一个新列表,一旦您从该方法返回,它就不再存在

如果您真的想存储树列表(我不知道您为什么要存储),您必须将其移动到字段声明:

private List<Tree> trees = new ArrayList<>();
private List trees=new ArrayList();
maxTree
值也存在类似问题;如果您想要存储它,并且这看起来与您的实例保持一致是合理的,那么您也必须将其移动到字段中。它不像上面的声明那样简单,因为您只知道该方法中的值是什么,但是它的调用不应该比它复杂得多。我把它作为练习留给读者。

不是在本文中

通常使用getter访问字段<代码>树在方法
build
中声明为局部变量。这意味着每次调用它时,您都会得到一个新列表,一旦您从该方法返回,它就不再存在

如果您真的想存储树列表(我不知道您为什么要存储),您必须将其移动到字段声明:

private List<Tree> trees = new ArrayList<>();
private List trees=new ArrayList();
maxTree
值也存在类似问题;如果您想要存储它,并且这看起来与您的实例保持一致是合理的,那么您也必须将其移动到字段中。它不像上面的声明那样简单,因为您只知道该方法中的值是什么,但是它的调用不应该比它复杂得多。我把它作为练习留给读者。

不是在本文中

通常使用getter访问字段<代码>树在方法
build
中声明为局部变量。这意味着每次调用它时,您都会得到一个新列表,一旦您从该方法返回,它就不再存在

如果您真的想存储树列表(我不知道您为什么要存储),您必须将其移动到字段声明:

private List<Tree> trees = new ArrayList<>();
private List trees=new ArrayList();

maxTree
值也存在类似问题;如果您想要存储它,并且这看起来与您的实例保持一致是合理的,那么您也必须将其移动到字段中。它不像上面的声明那样简单,因为您只知道该方法中的值是什么,但是它的调用不应该比它复杂得多。我将此作为练习留给读者。

通常
getter
用于类(非局部)变量。通常
getter
用于类(非局部)变量。通常
getter
用于类(非局部)变量。通常
getter
用于类(非局部)变量。您可以在列表上展开一点作为字段声明吗?我很确定这就是我需要做的。在我的模拟中,我需要另一个类