UML實例--HELL_WORLD(二)



Key Abstractions

In Java, the applet for printingHello, World! in a Web browser is quite simple:

import java.awt.Graphics;

class HelloWorld extends java.applet.Applet {

  public void paint (Graphics g) {

 g.drawString (“Hello, World!”, 10, 10);

}

   }

The first line of code:

import java.awt.Graphics;

makes the classGraphics directly available to the code that follows. Thejava.awtprefix specifies the Java package in which the classGraphicslives.

The second line of code:

class HelloWorld extends java.applet.Applet {

introduces a new class namedHelloWorld and specifies that it is a kind of class just likeApplet, which lives in the packagejava.applet.

The next three lines of code:

  public void paint (Graphics g) {

 g.drawString (“Hello, World!”, 10, 10);

}

declare an operation namedpaint, whose implementation invokes another operation, nameddrawString, responsible for printing the stringHello, World! at the given coordinates. In the usual object-oriented fashion, drawStringis an operation on a parameter namedg , whose type is the classGraphics.

Modeling this application in the UML is straightforward. As Figure 3-1 shows, you can represent the classHelloWorldgraphically as a rectangular icon. Itspaint operation is shown here as well, with all its formal parameters elided and its implementation specified in the attached note.

 

Note: The UML is not a visual programming language, although, as the figure shows, the UMLdoes allow -- but does not require -- a tight coupling to a variety of programming languages, such as Java. The UML is designed to allow models to be transformed into code and to allow code to be reengineered back into models. Some things are best written in the syntax of a textual programming language (for example, mathematical expressions), whereas other things are best visualized graphically in the UML (for example, hierarchies of classes).

 

關鍵抽象概念

Java中,在網絡瀏覽器中打印你好,世界!這類的小應用程序是非常簡單的,如下所示:

import java.awt.Graphics;

class HelloWorld extends java.applet.Applet {

  public void paint (Graphics g) {

 g.drawString (“Hello, World!”, 10, 10);

}

   }

第一行代碼

import java.awt.Graphics;

使得類Graphics在隨後的代碼中直接可用.Java.awt預定義包含有Graphics類的Java包.

第二行代碼

class HelloWorld extends java.applet.Applet {

定義一個新的名爲HelloWorld的類,它的描述擴展自包java.applet裏的類Applet

隨後的三行代碼

  public void paint (Graphics g) {

 g.drawString (“Hello, World!”, 10, 10);

}

聲明一個名爲paint的操作,它實現了另一個名爲drawString操作的調用,職責是在給定的位置打印“Hello, World!”.在通常的面向對象方法裏,drawString是參數g的一個操作,g的類型爲類Graphics

可以清楚的在UML中爲這個應用建模.如圖3-1所示,用一個矩形圖標來圖形化類HelloWorld.它的paint操作同樣顯示在這裏,省略了它的所有形參,它的實現在附加的備註中說明.

 

筆記:儘管UML不是一個可視化的編程語言,作爲圖形顯示,UML允許--但不是必需--與多種編程語言緊密連接,例如Java. UML被設計於允許模型轉化爲代碼,也允許代碼反向回模型.某些事物最好用文本式編程語言的語法來表述(如,數學表達式),然而,另一些事物更適合UML中形象化的圖形(如,類的結構層次).

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章