| 方法 | 描述 |
|---|---|
| boolean equals(Object obj) | 測試是否與參數具有相同型態及數值 |
舉例如下
class EqualsDemo {
public static void main(String[] args) {
Byte a1 = 25;
Short a2 = 24;
Integer a3 = 23;
Long a4 = 22l;
Float a5 = 21.0f;
Double a6 = 20.0;
Byte b1 = 25;
Short b2 = 23;
Integer b3 = 24;
Long b4 = 22l;
Float b5 = 20.0f;
Double b6 = 20.0;
System.out.println(a1.equals(b1));
System.out.println(a2.equals(b3));
System.out.println(a3.equals(b3));
System.out.println(a4.equals(b4));
System.out.println(a5.equals(b5));
System.out.println(a6.equals(b5));
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:EqualsDemo.java
功能:示範物件導向的基本觀念
作者:張凱慶
時間:西元 2010 年 10 月 */編譯後執行,結果如下

其中,第 17 行到第 22 行,其為呼叫 equals() 方法的地方,這裡,我們直接將回傳的布林值印出
System.out.println(a1.equals(b1)); System.out.println(a2.equals(b3)); System.out.println(a3.equals(b3)); System.out.println(a4.equals(b4)); System.out.println(a5.equals(b5)); System.out.println(a6.equals(b5));
若是型態與數值皆相同, equals() 回傳 true ,反之,回傳 false 。
| 中英文術語對照 | |
|---|---|
| 子類別 | subclass |
| 物件 | object |
沒有留言:
張貼留言