JSPCN主页 | JSP空间 | 网站制作 | JSP下载 | JSP论坛 | JSP教程 | 关于JSPCN | 联系我们
JSP虚拟主机,jsp空间,java空间,java虚拟空间,详细请点击进入
做最专业的JSP中文网站 当前位置首页--JAVA技术--JAVA类  
文章搜索:
关键字 标题
  
JSP中文网内容管理系统(JCMS)
JSP虚拟主机
网络笔记本
网摘,图片,笔记收藏
虚拟服务器

JSPCN文章目录分类
JSP配置[219]JSP基础[136]
中文问题[69]上传问题[27]
JAVABEAN[46]数据库[212]
文件操作[126]图片声音[17]
JSP其他[57]时间相关[16]
JAVAMAIL[72]STRUTS[144]
开发工具[28]教程系列[157]
JSP实例[89]
JAVA基础[421]APPLET[78]
JAVA网络[179]Applica[115]
Servlet[98]XML[163]
J2ME[257]J2EE[374]
考试相关[63]JAVA线程[90]
EJB[261]Swing[26]
Java API[141]声音图片[28]
异常处理[33]JAVA实例[290]
JAVA类[139]SUN[89]
Hibernate[6]JMX[8]
Spring[34]
本版推荐文章 
本版热点文章 
相关文章链接 
 
利用UML类图设计Java应用程序详解(二)
作者:未知     文章来源:www.jspcn.net
访问次数:478次     加入时间:2005年01月19日
在第一部分中,我们实现了5个类。在本部分中,我们接着说明如何利用UML类图来设计余下的各个类。为减少篇幅,本部分着重讲解UML类图及应用,对Java实现代码不再详细描述。
------------------------------------------------------------------
[url=http://www.java-cn.com/technology/tech_downs/2117_001.zip]下载本文全部代码
[/url]
------------------------------------------------------------------

六、CGPoint类

CGPoint类说明了如何利用非抽象类扩展抽象类。CGPoint类是CGObject的子类,CGPoint类扩展了 CGObject类,CGPoint类没有再它所继承的变量中增加变量,它所声明的方法只有构造函数和要求它实现的抽象方法。其类图如下:



screen.width-333)this.width=screen.width-333;">


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类的类图如下:



screen.width-333)this.width=screen.width-333;">


相应的代码为:
// 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中的第三个子类。其类图与代码分别如下:



screen.width-333)this.width=screen.width-333;">


// 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);

p = p.add(1,0);

}

}

public void describe() {

System.out.println("CGText "+location.toString()+" "+text);

}

}〉

以下是CGObject类、CGPoint类、CGBox类、CGText类及Point类之间的相互关系。注意CGObject类是抽象类,其类名用斜体表示。



screen.width-333)this.width=screen.width-333;">


九、KeyboardInput类

KeyboardInput类扩展了Java API的DataInputStream类,用来提供获取用户键盘输入的一系列常用的简单方法。其类图设计为:



screen.width-333)this.width=screen.width-333;">


代码为:
import java.lang.System;

import java.io.DataInputStream;

import java.io.InputStream;

import java.io.IOException;

//KeyboardInput.java

public 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 i.intValue();

}

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类属于内部类,当然你也可以将它单独作为一个类文件编辑、编译,其效果是一样的。

其中类与内部类之间的关系,用关联关系来表达,外部类用一个十字交叉圆圈表示,箭头指向内部类。如下图所示:



screen.width-333)this.width=screen.width-333;">


其代码实现为:
import java.lang.System;

import java.io.DataInputStream;

import java.io.IOException;

//CDrawApp.java

class CDrawApp {

public static void main(String args[]) throws IOException {

CDraw program = new CDraw();

program.run();

}

}



class CDraw {

// Variable declarations

static KeyboardInput kbd = new KeyboardInput(System.in);

BorderedPrintCGrid grid;



// Method declarations

CDraw() {

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类与相应类之间的关系为:



screen.width-333)this.width=screen.width-333;">


按照本文次序分别编译以上的10个大类,然后运行主程序CdrawApp即可。在程序运行时请注意:当选择增加点、框或者文本串后,选择Show Grid才能出现网格,并显示结果。

本文通过一个具体的程序开发过程,详细说明了如何使用UML类图来设计Java应用程序,使得程序开发可视化,文档标准化,便于相互协作与管理,是Java应用程序开发的方向 。





摘自:ZDnet   时间:2003年9月10日
 
Copyright © 2002-2005 JSPCN.net. All rights reserved.
JSP中文网    备案序号:蜀ICP备05001583号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号