我是
java的新手,我正在做一个简单的程序,但我不知道为什么我在我的程序中遇到错误,当我尝试使用超级…有人可以解释我或我的错误是什么,因为它不接受super.myCoord()我应该更改或添加什么?
- public class myCoord {
- private double coorX,coorY;
- public myCoord(){
- coorX = 1;
- coorY = 1;
- }
- public myCoord(double x,double y){
- coorX = x;
- coorY = y;
- }
- void setX(double x){
- coorX = x;
- }
- void setY(double y){
- coorY = y;
- }
- double getX(){
- return coorX;
- }
- double getY(){
- return coorY;
- }
- public String toString(){
- String nuevo = "("+coorX+","+coorY+")";
- return nuevo;
- }
- public class Coord3D extends myCoord{
- private double coorZ;
- Coord3D(){
- super.myCoord(); // ---> I got an error here !!
- coorZ = 1;
- }
- Coord3D(double x,double y,double z){
- super.myCoord(x,y); ---> Also here !!
- coorZ = z;
- }
- void setZ(double z){
- coorZ = z;
- }
- double getZ(){
- return coorZ;
- }
- }
解决方法
您应该使用点(.)运算符调用方法而不是构造函数.在这里,您使用点(.)调用超类’构造函数.
这就是为什么你会遇到这样的错误:
- The method myCoord() is undefined for the type myCoord
和
- The method myCoord(double,double) is undefined for the type myCoord
使用这些来调用你的超级构造函数:super();和super(x,y);如下所示.
- public class Coord3D extends myCoord {
- private double coorZ;
- Coord3D() {
- super(); // not super.myCoord(); its a constructor call not method call
- coorZ = 1;
- }
- Coord3D(double x,double z) {
- super(x,y); // not super.myCoord(x,y); its a constructor call not method call
- coorZ = z;
- }
- }