我有一个类形状,其构造函数在其构造函数中采用 (int Sides, Coodinates c, Color co) ,在类 Square 中我不想在构造函数中包含“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
的值。
关于java - 子类构造函数中的默认值要传递给父类(super class)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22738147/