figure.java
来自「国外的数据结构与算法分析用书」· Java 代码 · 共 66 行
JAVA
66 行
import dslib.list.LinkedListUos;
/** A figure with a name and a list of shapes that make up the figure. */
public class Figure extends Shape
{
/** Name of the figure. */
public String name;
/** Set the name to be newName.
Analysis: Time = O(1) */
public void setName(String newName)
{
name = newName;
}
/** The parts of the figure. */
protected LinkedListUos parts;
/** Make a new figure with name newName.
Analysis: Time = O(1) */
public Figure(String newName)
{
name = newName;
parts = new LinkedListUos();
x = 0;
y = 0;
}
/** Add s as a part of the figure.
Analysis: Time = O(1) */
public void insert(Shape s)
{
parts.insertLast(s);
}
/** Shift the position of each part of the figure by shiftX and shiftY.
Analysis: Time = O(number of parts of the figure) */
public void shift(int shiftX, int shiftY)
{
super.shift(shiftX, shiftY);
parts.goFirst();
while (!parts.after())
{
((Shape) parts.item()).shift(shiftX, shiftY);
parts.goForth();
}
}
/** String representation of the figure with indentation for nested parts.
Analysis: Time = O(number of parts to the figure) */
public String toString()
{
String result = "\n" + indent + name + " has parts: ";
indent += " ";
parts.goFirst();
while (!parts.after())
{
result += parts.item();
parts.goForth();
}
indent = indent.substring(0, indent.length() - 5);
return result;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?