繁体   English   中英

当我在子类中调用基类方法(此方法初始化字段)时,为什么字段未正确初始化?

[英]Why fields are not initialized correctly when I invoke a base class method (this method initialize the fields) in a subclass?

我有一个名为SimplePolygon的类,该类具有一个称为顶点的数组。 还有一个名为getNewPoly的工厂方法,该方法调用SimplePolygon构造函数并返回一个多边形。 在getNewPoly()内部,由于调用SimplePolygon构造函数而初始化了“顶点”。 我所做的是在名为ConvexPolygon的子类的构造函数中调用了getNewPoly,然后创建了一个凸凸对象,然后为凸凸多边形调用了顶点长度字段,这给了我0。这是怎么回事?

protected int n; // number of vertices of the polygon
protected Point2D.Double[] vertices; // vertices[0..n-1] around the polygon
protected static Line2D.Double[] edges;

 protected SimplePolygon(int size) {
    n = size;
    vertices = new Point2D.Double[n]; // creates array with n size. Elements
                                        // are doubles.

}

/** default no-parameter constructor */
protected SimplePolygon() {
    vertices = new Point2D.Double[0];
}

/********* public getters & toString ***************/

/**
 * static factory method constructs and returns an unverified
 * simple-polygon, initialised according to user provided input data. Runs
 * in O(n) time.
 * 
 * @return an unverified simple-polygon instance
 */
public static SimplePolygon getNewPoly() {
    Scanner fileIn = null;
    try {
        fileIn = new Scanner(new FileInputStream("vertices"));
    } catch (FileNotFoundException e) {
        System.out.println("File not found.");
        System.exit(0);
    }

    int size = 0; // Number of edges of the polygon.

    ArrayList<String> lines = new ArrayList<String>();

    // Adding every line from the file in an array list.
    while (fileIn.hasNextLine()) {
        lines.add(fileIn.nextLine());
        size++;
    }
    int noOfCoordinates = size * 2;
    Point2D.Double[] vertices = new Point2D.Double[size];
    ArrayList<java.lang.Double> coordinates = new ArrayList<java.lang.Double>();
    ArrayList<Point2D.Double> points = new ArrayList<Point2D.Double>();

    // Make every line a string tokenizer then split the string tokenizer
    // into two tokens then converting these tokens into double. Finally
    // adding these double values to 'coordinates' array list.
    for (int i = 0; i < lines.size(); i++) {
        StringTokenizer line = new StringTokenizer(lines.get(i));
        while (line.hasMoreTokens()) {
            double coordinate = java.lang.Double.parseDouble(line
                    .nextToken());
            coordinates.add(coordinate);
        }
    }

    // Taking the double values in 'coordinates' array list and create by
    // them
    // Point2D.Double then adding these Point2D into an array list.
    for (int z = 0; z < coordinates.size(); z += 2) {
        Point2D.Double point = new Point2D.Double(coordinates.get(z),
                coordinates.get(z + 1));
        points.add(point); // add the Point2D.Double points to 'points'
                            // array list.
    }

    SimplePolygon polygon = new SimplePolygon(size);

    // Finally adding Point2D to 'vertices' array.
    for (int x = 0; x < points.size(); x++) {
        vertices[x] = points.get(x);
    }

    for (int e = 0; e < polygon.vertices.length; e++) {
        polygon.vertices[e] = points.get(e);
    }

    edges = new Line2D.Double[size];

    for (int n = 0; n < vertices.length; n++) {
        if (n == vertices.length - 1)
            edges[n] = new Line2D.Double(vertices[n], vertices[0]);
        else
            edges[n] = new Line2D.Double(vertices[n], vertices[n + 1]);
    }

    return polygon;
}

子类的代码:

public class ConvexPolygon extends SimplePolygon {

        protected ConvexPolygon()
        {
            super.getNewPoly();

        }
    }

测试代码:

        ConvexPolygon poly = new ConvexPolygon();
        System.out.println(poly.vertices.length);

您似乎对代码的功能有错误的假设。 看一下你的构造函数:

 protected ConvexPolygon()
 {
        super.getNewPoly();
 }

这将首先调用超类的默认构造函数(因为您没有隐式调用另一个构造函数),将this.vertices初始化为一个空数组。 之后,您要从超类调用静态方法getNewPoly() ,创建一个与this不相关的SimplePolygon新实例。 由于您甚至没有保留对结果的引用,因此此行无效。

以这种方式应用工厂模式是行不通的,但是您可以使getNewPoly成为实例方法,可能将其命名为load (尽管要小心阴影变量名称!),然后从子类构造函数中调用它。

暂无
暂无

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

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