




已阅读5页,还剩30页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
利用UML类图设计Java应用程序详解(一)在第一部分中,我们实现了5个类。在本部分中,我们接着说明如何利用UML类图来设计余下的各个类。为减少篇幅,本部分着重讲解UML类图及应用,对Java实现代码不再详细描述。下载本文全部代码 六、CGPoint类CGPoint类说明了如何利用非抽象类扩展抽象类。CGPoint类是CGObject的子类,CGPoint类扩展了 CGObject类,CGPoint类没有再它所继承的变量中增加变量,它所声明的方法只有构造函数和要求它实现的抽象方法。其类图如下:Java实现代码为:/ CGPoint.javapublic class CGPoint extends CGObject / Method declarationspublic 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.javapublic class CGBox extends CGObject / Variable declarationsprotected Point lr; / Lower right corner of a box/ Method declarationspublic 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.javapublic class CGText extends CGObject / Variable declarationsString text;/ Method declarationspublic 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类是抽象类,其类名用斜体表示。九、KeyboardInput类 KeyboardInput类扩展了Java API的DataInputStream类,用来提供获取用户键盘输入的一系列常用的简单方法。其类图设计为:代码为:import java.lang.System;import java.io.DataInputStream;import java.io.InputStream;import java.io.IOException;/KeyboardInput.javapublic class KeyboardInput extends DataInputStream public KeyboardInput(InputStream inStream) super(inStream);public char getChar() throws IOException String line = readLine();if(line.length()=0) return ;return line.charAt(0);public String getText() throws IOException String line = readLine();return line;public int getInt() throws IOException String line = readLine();Integer i = new Integer(line);return Value();public Point getPoint() throws IOException System.out.print( x-coordinate: );System.out.flush();int x = getInt();System.out.print( y-coordinate: );System.out.flush();int y = getInt();return new Point(x,y);十、CDrawApp类主程序由CDrawApp类所构成。它包含main()方法,main()方法建立类CDraw的对象,然后调用该对象的run()方法。其中CDraw类属于内部类,当然你也可以将它单独作为一个类文件编辑、编译,其效果是一样的。其中类与内部类之间的关系,用关联关系来表达,外部类用一个十字交叉圆圈表示,箭头指向内部类。如下图所示:其代码实现为:import java.io.DataInputStream;import java.io.IOException;/CDrawApp.javaclass CDrawApp public static void main(String args) throws IOException CDraw program = new CDraw();program.run();class CDraw / Variable declarationsstatic KeyboardInput kbd = new KeyboardInput(System.in); BorderedPrintCGrid grid;/ Method declarationsCDraw() grid = new BorderedPrintCGrid();void run() throws IOException boolean finished = false;do char command = getCommand();switch(command)case P:addPoint();System.out.println();break;case B:addBox();System.out.println();break;case T:addText();System.out.println();break;case U:grid.deleteLastObject();System.out.println();break;case C:grid.clearGrid();System.out.println();break;case S:grid.show();break;case X:finished = true;default:System.out.println(); while (!finished);char getCommand() throws IOException System.out.println(CDrawApp P - Add a Point U - Undo Last Add);System.out.print (Main Menu B - Add a Box C - Clear Grid);System.out.println( X - Exit CDrawApp); System.out.print ( T - Add Text S - Show Grid );System.out.print ( Enter command: ); System.out.flush();return Character.toUpperCase(kbd.getChar();void addPoint() throws IOException System.out.println(Add Point Menu);System.out.println( Location:);Point p = kbd.getPoint();System.out.print( Character: );System.out.flush();char ch = kbd.getChar();if(ch= ) ch = +;CGPoint cgp = new CGPoint(p,ch);cgp.addToGrid(grid);void addBox() throws IOException System.out.println(Add Box Menu);System.out.println( Upper Left Corner:); Point ul = kbd.getPoint();System.out.println( Lower Right Corner:); Point lr = kbd.getPoint();System.out.print( Character: );System.out.flush();char ch = kbd.getChar();if(ch= ) ch = #;CGBox box = new CGBox(ul,lr,ch);box.addToGrid(grid);void addText() throws IOException System.out.println(Add Text Menu);System.out.println( Location:);Point p = kbd.getPoint();System.out.print( Text: );System.out.flush();String text = kbd.getText();CGText cgt = new CGText(p,text);cgt.addToGrid(grid);主程序CDrawApp类与相应类之间的关系为:按照本文次序分别编译以上的10个大类,然后运行主程序CdrawApp即可。在程序运行时请注意:当选择增加点、框或者文本串后,选择Show Grid才能出现网格,并显示结果。本文通过一个具体的程序开发过程,详细说明了如何使用UML类图来设计Java应用程序,使得程序开发可视化,文档标准化,便于相互协作与管理,是Java应用程序开发的方向 。利用UML类图设计Java应用程序详解(二)在第一部分中,我们实现了5个类。在本部分中,我们接着说明如何利用UML类图来设计余下的各个类。为减少篇幅,本部分着重讲解UML类图及应用,对Java实现代码不再详细描述。下载本文全部代码 六、CGPoint类CGPoint类说明了如何利用非抽象类扩展抽象类。CGPoint类是CGObject的子类,CGPoint类扩展了 CGObject类,CGPoint类没有再它所继承的变量中增加变量,它所声明的方法只有构造函数和要求它实现的抽象方法。其类图如下:Java实现代码为:/ CGPoint.javapublic class CGPoint extends CGObject / Method declarationspublic 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.javapublic class CGBox extends CGObject / Variable declarationsprotected Point lr; / Lower right corner of a box/ Method declarationspublic 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.javapublic class CGText extends CGObject / Variable declarationsString text;/ Method declarationspublic 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= limit) noOfWidths+;return noOfWidths * widthSurface;public double getTotalPrice(Surface aSurface) return getNoOfMeters(aSurface) * price;以上三个类之间的类图关系可以表示为如下图: 以下我们来详细分析类FlooringClient是如何发送消息给其它类,而实现方法的调用过程。并如何用UML序列图来描述这一序列过程。一、getNoOfMeters()方法让我们来看看是如何发送消息getNoOfMeters()的。对象Flooring要计算出需要多少米的材料才能贴满一定面积的表面,就需要对象Flooring与对象Surface之间相互作用。FlooringClient通过发送消息给getNoOfMeters()对象Flooring,在getNoOfMeters()方法的代码中,Flooring又发送消息给Surface而得到length和width。以上过程用UML序列图描述如下图:UML序列图描述了消息是如何在给对象间发送的。下面我们来详细解释以上UML序列图的含义,通过上述序列图,我们得知有以下8个过程:1. FlooringClient新建一个对象theSurface2. FlooringClient新建一个对象theFlooring3. FlooringClient发送一个消息给对象theFlooring,并以theSurface为变量4. theFlooring发送一个消息getLength()给theSurface5. theSurface发送一个回应给theFlooring6. theFlooring发送一个消息getWidth ()给theSurface7. theSurface发送一个回应给theFlooring8. theFlooring发送一个回应给FlooringClient二、getTotalPrice()方法 在FlooringClient程序中,我们有如下语句:doubl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年新能源汽车自动驾驶技术与交通法规协同发展风险评估报告
- 2025年六下语文试卷及答案
- 广西新风机采购合同模板(3篇)
- 小区安全培训新闻稿标题课件
- 专升本文科题真题及答案
- 仓储货架安全考试题
- 教育信息化基础设施建设中的物联网技术应用案例报告
- 互联网用户调研报告
- 2025食品供应合同样本
- 2025年心理咨询师之心理咨询师三级技能模考模拟试题(附答案)
- 6.2 人大代表为人民 第二课时 课件 2025-2026学年六年级道德与法治 上册 统编版
- 2025年甘肃省金川集团股份有限公司技能操作人员社会招聘400人考试参考试题及答案解析
- 4.2 遵守规则 课件 2025-2026学年 统编版道德与法治八年级上册
- T/CIE 189-2023硫化物全固态锂电池
- 2025年北京市单位劳动合同样本
- 借游戏账号合同5篇
- 广播稿的写法课件
- 2025年中职政治专业资格证面试技巧与答案解析大全
- 保密法课件教学课件
- 十八项核心医疗制度试题(附答案)
- 计生政策培训课件
评论
0/150
提交评论