java如何用类图设计_利用UML类图设计Java应用程序详解

41528d3028836879cd698677c3999917.gif利用UML类图设计Java应用程序详解

利用UML类图设计Java应用程序详解(一) 在第一部分中,我们实现了5个类。在本部分中,我们接着说明如何利用UML类图来设计余下的各个类。为减少篇幅,本部分着重讲解UML类图及应用,对Java实现代码不再详细描述。 下载本文全部代码 六、CGPoint类 CGPoint类说明了如何利用非抽象类扩展抽象类。CGPoint类是CGObject的子类,CGPoint类扩展了 CGObject类,CGPoint类没有再它所继承的变量中增加变量,它所声明的方法只有构造函数和要求它实现的抽象方法。其类图如下: Java实现代码为: // CGPoint.java public class CGPoint extends CGObject { // 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 // 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; iheight;++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; // 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 以下是CGObject类、CGPoint类、CGBox类、CGText类及Point类之间的相互关系。注意CGObject类是抽象类,其类名用斜体表示。 九、Keyboard类 Keyboard类扩展了Java API的DataStream类,用来提供获取用户键盘输入的一系列常用的简单方法。其类图设计为: 代码为: im

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值