剛感到好奇,各在 C# 與 JAVA 寫測試程式。
- C#:
Object myObj1 = 1; Object myObj2 = myObj1; Object myObj3 = 1; Console.WriteLine("myObj2.Equals(myObj1) = " + myObj2.Equals(myObj1)); Console.WriteLine("myObj3.Equals(myObj1) = " + myObj3.Equals(myObj1)); Console.WriteLine("(myObj2 == myObj1) = " + (myObj2 == myObj1)); Console.WriteLine("(myObj3 == myObj1) = " + (myObj3 == myObj1));
結果:
myObj2.Equals(myObj1) = True myObj3.Equals(myObj1) = True (myObj2 == myObj1) = True (myObj3 == myObj1) = False
- JAVA:
Object myObj1 = 1; Object myObj2 = myObj1; Object myObj3 = 1; System.out.println("myObj2.equals(myObj1) = " + myObj2.equals(myObj1)); System.out.println("myObj3.equals(myObj1) = " + myObj3.equals(myObj1)); System.out.println("(myObj2 == myObj1) = " + (myObj2 == myObj1)); System.out.println("(myObj3 == myObj1) = " + (myObj3 == myObj1));
結果:
myObj2.equals(myObj1) = true myObj3.equals(myObj1) = true (myObj2 == myObj1) = true (myObj3 == myObj1) = true