java使用抽象类

时间:2011-03-09 18:21:33

标签: java

说我想创建各种形状,包括直线,圆形,方形等 对于该行,我要求4个坐标(x,y)(x,y);

然后我有一个名为point的不同类,必须包含 2个字段:int x,y。 它必须有一个构造函数来初始化它的字段值和所需的get方法来读取这些值。

Shape是一个抽象类,可以被所有形状扩展。它应该包含形状必须具有的所有方法的签名。在扩展此抽象类的每个类中,每个方法的实现可能不同。

然后另一个类是一个形状(扩展Shape),它应该有这些字段:

- String TYPE = "Line"
- MyPoint p1,p2 
- Constructors 
- get methods for class fields such as getP1()
- Length() : calculates the length of a line returning a double rounded to two decimal points   
- Area() : which will return a the value zero as double
- renderShape(Graphics2D g2d) : used to draw the line to the screen. This method is provided for you, no need to edit it.
- printDetails(): will print the shape details , check typical input/output

我只是在徘徊,你怎么能从原始类中获取坐标并在Line类中打印出来,谢谢:)

1 个答案:

答案 0 :(得分:0)

定义抽象类形状。在这个类中定义一个抽象方法print(或类似的东西)。

在Line类中重写此方法。在那里你打印出四个坐标。

public abstract class Shape {
  public abstract void printCoordinates();
}

public class Line extends Shape {
   @Override
   public void printCoordinates() {
      System.out.println(.... print your coordinates here);
   }
}
相关问题