[英]constructor cannot be applied to given types;
我有一个名为 Shape 的基类,其中 Point 是另一个只包含 x 和 y 坐标的类:
abstract class Shape implements Comparable<Shape> {
public Point position;
public double area;
public Shape(Point p){
position.x = p.x;
position.y = p.y;
}
现在我有一个名为 Rectangle 的类,它扩展了 Shape 注意:位置是 Point 类型。
public Rectangle(Point p0, double w, double h) {
// Initializing the postition
super(Point(0,0));
position.x = p0.x;
position.y = p0.y;
// Initializing the height and the width
width = w;
height = h;
}
现在我知道需要先调用 Shape 构造函数,这是我使用 super 所做的,但现在编译器告诉它找不到符号。 你如何继续解决这个问题? 这是错误:
./Rectangle.java:21: error: constructor Shape in class Shape cannot be applied to given types;
{ // Initialzing of the postition
^
required: Point
found: no arguments
reason: actual and formal argument lists differ in length
1 error
您的代码将是:
public Rectangle(Point p0, double w, double h)
{
// Initialzing of the postition
super(p0);
// Initialzing the height and the width
width = w;
height = h;
}
因为你的 super 是Shape
构造函数。
你确定Rectangle
扩展Shape
吗? 如果你可以发布你的类Rectangle
定义
你可以做这样的事情..
public Rectangle(Point p0, double w, double h) {
// Initializing the postition
super(new Point(0,0));
position.x = p0.x;
position.y = p0.y;
// Initializing the height and the width
width = w;
height = h;
}
您错过了超级构造函数中的 new 关键字。
我猜你的课程是这样的:
public class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Shape {
public Point position;
public Shape(Point p) {
position.x = p.x;
position.y = p.y;
}
}
而 Rectangle 类的超级构造函数应该接收一个 Point 对象,因为它继承自 Shape,它的构造函数参数是一个 Point,所以这个类应该是这样的:
public class Rectangle extends Shape{
public double width;
public double height;
public Rectangle(Point p0, double w, double h) {
// Initialzing of the postition
super(p0);
// Initialzing the height and the width
width = w;
height = h;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.