利用UML类图设计Java应用程序详解..doc
文本预览下载声明
利用UML类图设计Java应用程序详解(一)在第一部分中,我们实现了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());
}
}
七、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; iwidth; ++i) {
grid.setCharAt(drawCharacter,topRow);
grid.setCharAt(drawCharacter,bottomRow);
topRow = topRow.add(1,0);
bottomRow = bottomRow.add(1,0);
}
Point leftCol = new Point(location);
Point rightCol = new Point(lr.getX(),location.getY());
for(int i=0;iheight;++i){
grid.setCharAt(drawCharacter,leftCol);
grid.setCharAt(drawCharacter,rightCol);
leftCol = leftCol.add(0,1);
rightCol = rightCol.add(0,1);
}
}
public
显示全部