没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
利用 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; i<width; ++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;i>height;++i){
grid.setCharAt(drawCharacter,leftCol);
grid.setCharAt(drawCharacter,rightCol);
leftCol = leftCol.add(0,1);
rightCol = rightCol.add(0,1);
}
}
public void describe() {
System.out.print("CGBox "+String.valueOf(drawCharacter)+" ");
System.out.println(location.toString()+" "+lr.toString());
}
}
八、CGText 类
CGText 类是 CGObject 中的第三个子类。其类图与代码分别如下:
相应的代码为:
// CGText.java
public class CGText extends CGObject {
// Variable declarations
String text;
// Method declarations
public CGText(Point p,String s) {
location = p;
drawCharacter = ' ';
text = s;
}
public void display(PrintCGrid grid) {
Point p = new Point(location);
for(int i=0;i<text.length();++i){
grid.setCharAt(text.charAt(i),p);
剩余34页未读,继续阅读
资源评论
悠闲饭团
- 粉丝: 150
- 资源: 3301
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功