在第一部分中,我们实现了5个类。在本部分中,我们接着说明如何利用UML类图来设计余下的各个类。为减少篇幅,本部分着重讲解UML类图及应用,对Java实现代码不再详细描述。
下载本文全部代码六、CGPoint类
CGPoint类说明了如何利用非抽象类扩展抽象类。CGPoint类是CGObject的子类,CGPoint类扩展了 CGObject类,CGPoint类没有再它所继承的变量中增加变量,它所声明的方法只有构造函数和要求它实现的抽象方法。其类图如下:

Java实现代码为:
// CGPoint.java
public class CGPoint extends CGObject {
// Method declarations
public CGPoint(int x, int y,char ch) {
location = new Point(x,y);
drawCharacter = ch;
}
public CGPoint(int x, int y) {
this(x,y,'+');
}
public CGPoint(Point p) {
this(p.getX(),p.getY(),'+');
}
public CGPoint(Point p,char ch) {
this(p.getX(),p.getY(),ch);
}
public void display(PrintCGrid grid) {
grid.setCharAt(drawCharacter,location);
}
public void describe() {
System.out.print("CGPoint "+String.valueOf(drawCharacter)+" ");
System.out.println(location.toString());
}
}
