[英]Default values in constructors of a subclass, to pass onto a superclass?
我有一个带有构造函数的类形状,它在构造函数中采用(int Sides,Coodinates c,Color co),在类方块中我不想在构造函数中使用“int Sides”,我该怎么做?
//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,我是否正确?
public Square(Coordinates c, Color co, int w, int l) {
super(4, c, co);
this.w = w;
this.l = l;
}
是的,你会是对的。 但是,您还需要指定w
和l
的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.