Super()不能在我的扩展类上工作

时间:2014-01-25 13:51:38

标签: java

我是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;
        }

    }

4 个答案:

答案 0 :(得分:9)

super调用super()的构造函数,无论是参数还是不参数。在你的情况下:

public class Coord3D extends myCoord{
    private double coorZ;

    Coord3D(){
        super();
        coorZ = 1;
    }

    Coord3D(double x, double y, double z){
        super(x,y);
        coorZ = z;

    }

    // rest of the class snipped
}

答案 1 :(得分:5)

您应该使用点(.)运算符调用方法,而不是构造函数。在这里,您使用点(.)调用超类'构造函数。

这就是为什么你会遇到这样的错误:

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 y, double z) {
        super(x,y);  // not super.myCoord(x,y); its a constructor call not method call
        coorZ = z;
    }
}

答案 2 :(得分:2)

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(); // ---> I got an error here !!
            coorZ = 1;
        }

        Coord3D(double x, double y, double z){
            super(x,y);  ---> Also here !!
            coorZ = z;

        }

        void setZ(double z){
            coorZ = z;
        }

        double getZ(){
            return coorZ;
        }

    }

答案 3 :(得分:1)

super()


super(x,y);

他们应该是那样的,你正在调用构造函数