Java 在visit函数中使用变量
在下面的代码中,我试图访问'GMLWalker'访问函数中的'singleBuilding'变量。我得到的错误是无法引用封闭范围中定义的非最终局部变量singleBuilding。关于我如何做到这一点的任何线索Java 在visit函数中使用变量,java,visitor,Java,Visitor,在下面的代码中,我试图访问'GMLWalker'访问函数中的'singleBuilding'变量。我得到的错误是无法引用封闭范围中定义的非最终局部变量singleBuilding。关于我如何做到这一点的任何线索 private FeatureWalker IterateGroundSurface(BuildingClass singleBuilding){ FeatureWalker groundWalker = new FeatureWalker(){ public
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;
}
谢谢,如果要在内部类中访问它,需要将其设置为最终版本。同样,如果将其设置为最终设置,则无法重新初始化它
private FeatureWalker IterateGroundSurface(final BuildingClass singleBuilding){
}
Java至少在Java8之前不支持真正的闭包。深入解释您的问题。感谢您的回答,看起来我将不得不使用全局变量。如果您需要知道原因,那么您可以按照@Mick Mnemonic的评论进行操作。这是一个问题。我必须不断更新singleBuilding变量。作为一个临时解决方案,我使用一个全局变量来处理这个问题。我看了这个:。但我认为这是再次,使用全局变量的方法。