三、同一個(gè)類的兩個(gè)對(duì)象之間的交互 一個(gè)對(duì)象可以與同一個(gè)類的另一個(gè)對(duì)象交互從而完成程序所規(guī)定的任務(wù)。假如我們?cè)赟urface類中增加一個(gè)比較面積的方法。程序代碼為: public int compareAreas(Surface theOtherSurface){
final double precision=0.00001;
double area1=getArea();
double area2=theOtherSurface.getArea();
if (Math.abs(area2-area1) else if(area1>area2) return –1;
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發(fā)生交互從而得到結(jié)果result。首先它計(jì)算出自己的面積,然后計(jì)算出surface2的面積,最后再比較它們兩個(gè)之間的面積的大小。 以上過程用UML序列圖可以描述為下圖: