我编写了一个StickFigure类,它是对熟悉的“ stick person”的简化表示。为了在图形显示中显示简笔画,我将其简化为基本要素,并使用圆形,三角形和矩形构建了它。
StickFigure具有xPos和yPos,它们也是仅有的2个实例变量。
到目前为止,我已经声明了另外三个实例变量head,body和leg,它们将引用Circle,Triangle和Rectangle的实例。我想我做得正确
Circle aHead = new Circle();
Triangle aBody = new Triangle();
Rectangle aLeg = new Rectangle();
我现在正在尝试为StickFigure创建构造函数,以便将Head初始化为直径为30且颜色为Colour.PINK的Circle实例。我不确定该怎么做!以下是我到目前为止所做的
public StickFigure()
{
super();
this.setXPos(25);
this.setYPos(220);
}
到目前为止的完整代码:
`public class StickFigure { /*Instance variables*/
private int xPos;//The horizontal position of a StickFigure
private int yPos;//The vertical position of a StickFigure
//我的声明
Circle aHead = new Circle();Triangle aBody = new Triangle();Rectangle aLeg = new Rectangle();
/ **
* StickFigure类的对象的构造方法,
*在图形显示的左下角附近提供默认的数字。
*
* /
public StickFigure()
{
super();
this.setXPos(25);
this.setYPos(220;
{
最佳答案
这是一个可能且非常基本的解决方案:
enum Colour {
BLACK,
WHITE
}
class BodyPart {
Colour colour;
public BodyPart() {}
public BodyPart(Colour colour) {
this.colour = colour;
}
}
class Circle extends BodyPart {
private int diameter;
public Circle(Colour colour, int diameter) {
super(colour);
this.diameter = diameter;
}
//getter and setter
}
class Triangle extends BodyPart {
//whatever you want here
}
class Rectangle extends BodyPart {
//whatever you want here
}
class StickFigure {
private Circle head;
private Triangle body;
private Rectangle leg; // it could be a list of legs :D
public StickFigure(Circle head, Triangle body, Rectangle leg) {
super();
this.head = head;
this.body = body;
this.leg = leg;
}
public static void main(String[] args) {
StickFigure sf = new StickFigure(new Circle(Colour.BLACK, 10), new Triangle(), new Rectangle());
}
}