利用UML 类图设计Jav a 应用程序详解(一) 在第一部分中,我们实现了5 个类。在本部分中,我们接着说明如何利用UML 类图来设计余下的各个类。为减少篇幅,本部分着重讲解UML 类图及应用,对Jav a 实现代码不再详细描述。 下载本文全部代码 六、CGPoint 类 CGPoint 类说明了如何利用非抽象类扩展抽象类。CGPoint 类是 CGObject 的子类,CGPoint类扩展了 CGObject 类,CGPoint 类没有再它所继承的变量中增加变量,它所声明的方法只有构造函数和要求它实现的抽象方法。其类图如下: Jav a 实现代码为: // 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()); } } 七、CGBox 类 CGBox 类也扩展了 CGObject 类。CGBox 类提供了在网格上显示矩形的附加变量。CGBox类的类图如下: 相应的代码为: // CGBox.java public class CGBox extends CGObject { // Variable declarations protected Point lr; // Lower right corner of a box // Method declarations public CGBox(Point ulCorner, Point lrCorner,char ch) { location = ulCorner; lr = lrCorner; drawCharacter = ch; } public CGBox(Point ulCorner, Point lrCorner) { this(ulCorner,lrCorner,'#'); } public void display(PrintCGrid grid) { int width = lr.getX() - location.getX() + 1; int height = lr.getY() - location.getY() + 1; Point topRow = new Point(location); Point bottomRow = new Point(location.getX(),lr.getY()); for(int i=0; i