if (Math.abs(area2-area1)<precision) return 0;
else if(area1>area2) return –1;
else return 1;
}
在主程序FlooringClient中,我們可以實現如下代碼:
Surface surface1=new Surface("A",5,4);
Surface surface2=new Surface("B",4,4);
Int result=surface1.compareAreas(surface2);
If (result<0) System.out.println(surface1.getName()+"is the smaller one");
else If (result>0) System.out.println(surface2.getName()+"is the smaller one");
else System.out.println("The surface has the same area");
從以上程序中可以看出,surface1與surface2發生交互從而得到結果result。首先它計算出自己的面積,然后計算出surface2的面積,最后再比較它們兩個之間的面積的大小。
以上過程用UML序列圖可以描述為下圖:

以上詳細說明了如何利用UML序列圖來描述各類之間的對象或同一類不同之間的對象相互之間的交互序列過程。是Java應用程序面向對象設計過程中的一個重要方面。