在访问函数中使用变量

时间:2015-04-26 13:20:23

标签: java visitor

在下面的代码中,我正在尝试访问“单一建筑”' “GMLWalker”中的变量'参观功能。我收到错误"无法引用封闭范围中定义的非最终局部变量singleBuilding"。关于如何实现这一目标的任何线索。

private FeatureWalker IterateGroundSurface(BuildingClass singleBuilding){
    FeatureWalker groundWalker = new FeatureWalker(){

        public void visit(GroundSurface groundSurface){
            GMLWalker gmlWalker = new GMLWalker(){
                public void visit(LinearRing linearRing){

                    if(linearRing.isSetPosList()){
                        SurfaceMember surfaceMember = new SurfaceMember();

                        DirectPositionList posList = linearRing.getPosList();
                        List<Double> points = posList.toList3d();

                        List<CoordinateClass> polygonfloor = new ArrayList<CoordinateClass>();
                        PolygonClass poly = new PolygonClass();
                        for(int i=0 ; i<points.size() ;i+=3){
                            double[] vals = new double[]{points.get(i) , points.get(i+1),points.get(i+2)};
                            //System.out.println(vals[0]+" "+vals[1]+" "+vals[2]);
                            CoordinateClass coord = new CoordinateClass(vals);
                            polygonfloor.add(coord);
                        }
                        poly.setPolygon(polygonfloor);
                        surfaceMember.setPolygon(poly);
                        singleBuilding.setSurfacePolygon(surfaceMember);


                        //surfacePolygons.add(buildingSurfacePolygon);
                    }
                }
            };
            groundSurface.accept(gmlWalker);
        }
    };
    return groundWalker;
}

谢谢,

1 个答案:

答案 0 :(得分:0)

如果要在内部类中访问它,则需要将其设为final。再次,如果你把它作为最终,那么你将无法重新初始化它。

private FeatureWalker IterateGroundSurface(final BuildingClass singleBuilding){
}