Java 子类的构造函数中的默认值传递到超类?
我有一个类形状,它的构造函数中有(int边,坐标c,颜色co),在类正方形中,我不想在构造函数中有“int边”,我该怎么做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
//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;
}