【发布时间】:2011-03-09 18:21:33
【问题描述】:
假设我想创建各种形状,包括直线、圆形、正方形等 对于这条线,我要求 4 个坐标 (x,y) (x,y);
然后我有一个名为 point 的不同类,它必须包含 2个字段:int x,y。 它必须有一个构造函数来初始化其字段的值以及读取这些值所需的 get 方法。
Shape 是一个可以被所有形状扩展的抽象类。它应该包含一个形状必须具有的所有方法的签名。在扩展这个抽象类的每个类中,每个方法的实现可能不同。
然后另一个类 Line 是一个形状(扩展 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 类中打印出来,谢谢 :)
【问题讨论】:
-
闻起来像作业,不工作。
-
你的问题似乎有点不清楚,我觉得答案可能部分取决于你如何实现 Shape 或你认为你的原始类是什么。听起来您只想访问 p1 和 p2 内的坐标并打印它们?
-
为了获得灵感,您可能需要查看
java.awt.geom中的类层次结构(以及java.awt.Shape和java.awt中的一些其他类)。
标签: java