简体   繁体   English

我的getter函数放在哪里?

[英]Where do I put my getter function?

The class below is a context builder which places Tree objects in geographic space on a grid. 下面的类是一个上下文生成器,它将Tree对象放置在网格的地理空间中。 I have created an array list of trees objects with various suitability values and ids: 我创建了具有各种适用性值和id的树对象的数组列表:

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; 

}


}   

I believe I can use a getter to access the list in other classes. 我相信我可以使用吸气剂来访问其他课程中的列表。 Something like this... 像这样

public ArrayList<Tree> getList() {
return trees;
}

But my question is: Where do I put the code above? 但是我的问题是:我将代码放在哪里? I get an error whenever I place it, specifically with "return trees;". 每当我放置它时,都会出现错误,特别是“ return tree;”。

In addition: Can I also use a getter to get the maxTree value from the list? 另外:我还可以使用getter从列表中获取maxTree值吗?

Not in this context. 不在这种情况下。

One generally uses a getter to access a field; 通常,使用吸气剂来访问字段。 trees is declared as a local variable in the method build . build方法中将trees声明为局部变量。 This means that every time it's called, you get a new list, and it no longer exists once you've returned from the method. 这意味着每次调用它时,您都会得到一个新列表,并且从该方法返回后,该列表将不再存在。

If you really want to store the trees list (and I'm not sure why you would want to), you would have to move it to a field declaration: 如果您确实要存储树列表(我不确定为什么要这么做),则必须将其移至字段声明:

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

A similar issue exists with the maxTree value; maxTree值也存在类似问题; if you want to store that, and that does seem like something reasonable to keep with your instance, then you'd have to move that to a field as well. 如果您想存储它,并且看起来确实适合保留您的实例,那么您也必须将其移动到字段中。 It's not as straightforward as the declaration above, as you'd only know what the value is inside of that method, but its invocation shouldn't be much more complicated than it. 它不像上面的声明那样简单明了,因为您只知道该方法内部的值,但是它的调用不应该比它复杂得多。 I leave that as an exercise to the reader. 我把它留给读者练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM