我一直认为,每当您创建静态嵌套类的实例时,它都应该类似于:

Outerclass.StaticNestedClass myObject = new Outerclass.StaticNestedClass();


下面的代码(我省略了一些我认为不相关的代码)没有使用封闭的类名。 GeneratedData是ObjectBuilder类的静态嵌套类。我为什么看不到:

ObjectBuilder.GeneratedData = ObjectBuilder.createMallet()


相反,有:

GeneratedData generatedData = ObjectBuilder.createMallet();


这是java文件...

public class Mallet {
public Mallet(float radius, float height, int numPointsAroundMallet) {

    GeneratedData generatedData = ObjectBuilder.createMallet(new Point(0f,
        0f, 0f), radius, height, numPointsAroundMallet);

    // lots of code omitted

}
}


这是定义静态嵌套类的类。如您所见,方法createMallet()创建一个类型为GeneratedData的对象,并且上面的Mallet类使用该方法而不是构造函数。

class ObjectBuilder {

static interface DrawCommand {
    void draw();
}

static class GeneratedData {
    final float[] vertexData;
    final List<DrawCommand> drawList;

    GeneratedData(float[] vertexData, List<DrawCommand> drawList) {
        this.vertexData = vertexData;
        this.drawList = drawList;
    }
}

static GeneratedData createMallet(
    // lots of code omitted
    return builder.build();
}

private final float[] vertexData;

private ObjectBuilder(int sizeInVertices) {
    vertexData = new float[sizeInVertices * FLOATS_PER_VERTEX];
}

private GeneratedData build() {
    return new GeneratedData(vertexData, drawList);
}
}

最佳答案

因为您可以在课后import xyz.ObjectBuilder.GeneratedData并随后使用GeneratedData

10-06 13:20