Java 为什么在调用子类中的基类方法(该方法初始化字段)时字段未正确初始化?
我有一个名为SimplePolygon的类,它有一个称为顶点的数组字段。还有一个名为getNewPoly的工厂方法,它调用SimplePolygon构造函数并返回多边形。在getNewPoly中,“顶点”是通过调用SimplePolygon构造函数初始化的。 我所做的是在一个名为ConvexPolygon的子类的构造函数中调用getNewPoly,然后创建一个ConvexPolygon对象,然后调用ConvexPolygon的顶点长度字段,它给了我0。那怎么了Java 为什么在调用子类中的基类方法(该方法初始化字段)时字段未正确初始化?,java,Java,我有一个名为SimplePolygon的类,它有一个称为顶点的数组字段。还有一个名为getNewPoly的工厂方法,它调用SimplePolygon构造函数并返回多边形。在getNewPoly中,“顶点”是通过调用SimplePolygon构造函数初始化的。 我所做的是在一个名为ConvexPolygon的子类的构造函数中调用getNewPoly,然后创建一个ConvexPolygon对象,然后调用ConvexPolygon的顶点长度字段,它给了我0。那怎么了 protected int n;
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;
}
测试代码:
ConvexPolygon poly = new ConvexPolygon();
System.out.println(poly.vertices.length);
似乎您对代码的功能有错误的假设。看看你的构造器:
protected ConvexPolygon()
{
super.getNewPoly();
}
这将首先调用超类的默认构造函数,因为您不是隐式调用另一个构造函数,而是将This.vertices初始化为空数组。然后,从超类调用静态方法getNewPoly,创建与此无关的SimplePolygon的新实例。由于您甚至没有保留对结果的引用,因此此行没有任何效果
以这种方式应用工厂模式将不起作用,但您可以使getNewPoly成为一个实例方法,可能会将其命名为load,但要小心隐藏变量名!,然后从子类构造函数调用它。创建一个最小的测试用例,包含相关的类定义,并且没有不相关的多边形生成代码。超类构造函数保证在子类构造函数执行之前运行;构造函数的问题只存在于所描述的方向之外——主要是当超类构造函数试图访问在子类构造函数中分配的成员变量时。无论如何,我怀疑问题在于调用了默认构造函数,它创建了一个长度为0的数组。。但我并没有改变这一点。不要描述代码,共享。1看起来你不需要调用super.getNewPoly,getNewPoly应该足够了。2看起来您正在成功调用构造函数SimplePolygon。。。这将创建一个零元素数组。如果有疑问。。。然后使用调试器。在getNewPoly中设置一个断点并逐步完成代码!
protected ConvexPolygon()
{
super.getNewPoly();
}