【发布时间】:2014-05-09 09:49:18
【问题描述】:
我有一个带有构造函数的类形状,在其构造函数中采用 (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 是否正确?
【问题讨论】:
标签: java class object constructor superclass