gpt4 book ai didi

Java - 将对象作为构造函数中的参数传递......来自父类(super class)

转载 作者:行者123 更新时间:2023-11-29 05:52:39 24 4
gpt4 key购买 nike

我有这些类:一个名为“Shape”的抽象类,一个类 Point extends Shape,一个类 Circle extends Point

我无法改变这种关系。我需要做的是在 Circle 中创建一个构造函数,它接收一个对象 Point 和一个 double 对象作为参数。我已经创建了一个对象点,称为点我已经试过了:

public Circle (Point objPunto, double valorRadio){   
Pointp = new Point(objPunto.getX(), objPunto.getY());
setRadio(valorRadio);}

Circulo circulo2 = new Circulo(point, 3);

但它似乎不起作用。它向我展示了这个:隐式 super 构造函数 Punto() 未定义。必须显式调用另一个构造函数

我也使用 Super 尝试了一些奇怪的事情,但那也没有用。我开始认为这是可以做到的,如果是的话...有人知道为什么吗?

谢谢!

点代码

    public class Point extends Shape {

private int x;
private int y;

public Point( int valorX, int valorY ){
x = valorX;
y = valorY;
}

public void setX( int valorX ){
x = valorX;
}

public int getX(){
return x;
}

public void setY( int valorY ){
y = valorY;
}

public int getY(){
return y;
}

public String getName(){
return "Punto";
}

public String describe(){
return "[" + obtenerX() + ", " + obtenerY() + "]";
}
}

最佳答案

您的圈子正在尝试调用 super(); 作为第一步,但是 Point 没有默认的无参数构造函数。您必须调用将参数作为 Circle 构造函数的第一行的 super 方法。

尝试 super(objPunto.getX(), objPunto.getY()); 作为 Circle 构造函数中的第一个调用

关于Java - 将对象作为构造函数中的参数传递......来自父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com