[英]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. super
类Design05
类没有构造函数,该构造函数将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.