Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 子类的构造函数中的默认值传递到超类?_Java_Class_Object_Constructor_Superclass - Fatal编程技术网

Java 子类的构造函数中的默认值传递到超类?

Java 子类的构造函数中的默认值传递到超类?,java,class,object,constructor,superclass,Java,Class,Object,Constructor,Superclass,我有一个类形状,它的构造函数中有(int边,坐标c,颜色co),在类正方形中,我不想在构造函数中有“int边”,我该怎么做 //Shape public Shape(int sides, Coordinates c, Color co) { this.sides = sides; this.c = c; this.co = co; } //Square //Like this is gives me an error saying I need to include s

我有一个类形状,它的构造函数中有(int边,坐标c,颜色co),在类正方形中,我不想在构造函数中有“int边”,我该怎么做

//Shape
public Shape(int sides, Coordinates c, Color co) {
    this.sides = sides;
    this.c = c;
    this.co = co;
}

//Square
//Like this is gives me an error saying I need to include sides into my
//constructor. I don't want to request the number of sides when creating
//a square, it will always be 4, how do I do this?
public Square(Coordinates c, Color co, int w, int l) {
    //blah blah
}
假设我必须使用super(4,c,co)传递值4,这是否正确


是的,你是对的。但是,您还需要分配
w
l

的值,是的,我只是没有键入,哈哈。谢谢你帮我澄清:)
public Square(Coordinates c, Color co, int w, int l) {
    super(4, c, co);
    this.w = w;
    this.l = l;
}