简体   繁体   English

构造函数 <Class> 未定义。 不能使用 <super(arg)> 在java中

[英]The constructor <Class> is undefined. Can't use <super(arg)> in java

I'm a complete newbie in Java. 我是Java的新手。 I can't understand why Java doesn't allow me to use the keyword super(arg). 我不明白为什么Java不允许我使用关键字super(arg)。 OBS: Design05C is subclass of Design05. OBS:Design05C是Design05的子类。

public class Design05C extends Design05 {
    private double x;
    private double y;

    public Design05C(char typeCoord, double xCartesian, double yCartesian) {
        super(typeCoord);
        this.x = xCartesian;
        this.y = yCartesian;
    }   

    //Instance methods **************************************************    
    public double getX()  
    {
        if(typeCoord == 'C') 
            return x;
        else 
            return (Math.cos(Math.toRadians(y)) * x);
    }   

    public double getY()
    {
        if(typeCoord == 'C') 
            return y;
        else 
            return (Math.sin(Math.toRadians(y)) * x);
    }    
}

This is the superclass, Design05: 这是超类Design05:

public class Design05
{
    public char typeCoord;
    public Design05C designC;
    public Design05P designP;

    public Design05(char type, Design05C dCartesian, Design05P dPolar)
    {
        if(type != 'C' && type != 'P')
            throw new IllegalArgumentException();
        typeCoord = type;
        designC = dCartesian;
        designP = dPolar;
    }
}

Thank you!!! 谢谢!!!

Possibility reason is, 可能的原因是,

super class Design05 class don't have a constructor which takes a char as argument. superDesign05类没有构造函数,该构造函数将char作为参数。

Edit 编辑

Since the constructor 自构造函数

 public Design05(char type, Design05C dCartesian, Design05P dPolar)
    {
        if(type != 'C' && type != 'P')
            throw new IllegalArgumentException();
        typeCoord = type;
        designC = dCartesian;
        designP = dPolar;
    }

taking three argument ,but you are passing only one parameter here is 接受三个参数,但是您只传递了一个参数

super(typeCoord);

So add another constructor to your super class such that it takes char alone also. 因此,将另一个构造函数添加到您的超类中,这样它也可以单独使用char

public class Design05
{
    public char typeCoord;
    public Design05C designC;
    public Design05P designP;

    public Design05(char type, Design05C dCartesian, Design05P dPolar)
    {
        if(type != 'C' && type != 'P')
            throw new IllegalArgumentException();
        typeCoord = type;
        designC = dCartesian;
        designP = dPolar;
    }

   public Desgin05(char type){

   typeCoord = type;

  }
}

test that Design05 has created this contructor 测试Design05是否已创建此构造器

public Desgin05(char value){

this.value = value;

}

if there isn't constructor, you need to create. 如果没有构造函数,则需要创建。

There is no constructor in Design05 class which takes a single char argument. Design05类中没有使用单个char参数的构造函数。 You will have to change your call to super constructor from this 您将不得不从此更改对超级构造函数的调用

  super(typeCoord);

to this 对此

  super(typeCoord, xCartesian, yCartesian);

Then your code would work. 然后您的代码将起作用。 Try. 尝试。

EDIT: 编辑:

Your design itself is incorrect. 您的设计本身是不正确的。 You cannot add the subclasses objects in the constructor of the super class. 您不能在超类的构造函数中添加子类对象。 This wont work. 这不会工作。 Remove the subclass objects from the constructor. 从构造函数中删除子类对象。

If you want to add the subclass objects inside the super class object, then keep references of them and initialize them after the construction of the objects. 如果要在超类对象中添加子类对象,则保留它们的引用并在构造对象后对其进行初始化。 And not in the constructor. 而不是在构造函数中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 隐式超类构造函数MyNumber()是未定义的。 必须调用另一个构造函数 - Implicit super class constructor MyNumber() is undefined. Must invoke another constructor Java 反射无法实例化没有 arg 构造函数的私有嵌套类 - Java reflection can't instantiate a private nested class with no arg constructor Java ByteArrayInputStream隐式超级构造函数未定义。 必须显式调用另一个构造函数 - Java ByteArrayInputStream Implicit super constructor is undefined. Must explicitly invoke another constructor Java的。 隐式超级构造函数Settore()是未定义的。 必须显式调用另一个构造函数 - Java. Implicit super constructor Settore() is undefined. must explicitly invoke another constructor 即使没有arg构造函数,隐式超级构造函数也是不确定的? - implicit super constructor is undefined even with no arg constructor? 隐式超级构造函数 Shape2D() 未定义。 关于“包括 Java.awts.Color” - implicit super constructor Shape2D() is undefined. in regards to with “include Java.awts.Color” 无法使用Scanner类,构造函数未定义,方法未定义 - Can't use Scanner class, constructor is undefined, method is undefined 构造函数使用 this 和 super Java - Constructor use this and super Java Java类如何没有no-arg构造函数? - How can a Java class have no no-arg constructor? 隐式超级构造函数Teleporter()未定义。 必须显式调用另一个构造函数? - Implicit super constructor Teleporter() is undefined. Must explicitly invoke another constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM