对象由OBJECT_ID定义,OBJECT_ID是唯一的。如果A和B是对象
A == B为真,那么它们是相同的对象,它们有相同的数据和方法,但是,这也是成立的:
A.object_id == b.object_id
如果
A = (B)为真,这意味着两个对象处于相同的状态,但这并不意味着A和B完全相同。
字符串是对象。
请注意==和Equals操作符是自反的、simetric的、tranzitive的,因此它们是等价关系(使用关系代数术语)。
这意味着:
如果A, B和C是对象,则:
(1) A == A总是正确的;A. equals (A)总是正确的(反身性)
(2)如果A == B那么B == A;如果A等于(B)那么B等于(A) (simetry)
(3)如果A == B且B == C,则A == C;如果a .等于(B), B.等于(C),则a .等于(C)(静止性)
另外,你可以注意到这也是真的:
(A == B) => (A = (B)),但反过来就不对了。
A B =>
0 0 1
0 1 1
1 0 0
1 1 1
Example of real life:
Two Hamburgers of the same type have the same properties: they are objects of the Hamburger class, their properties are exactly the same, but they are different entities. If you buy these two Hamburgers and eat one, the other one won't be eaten. So, the difference between Equals and ==:
You have hamburger1 and hamburger2. They are exactly in the same state (the same weight, the same temperature, the same taste), so hamburger1.Equals(hamburger2) is true. But hamburger1 == hamburger2 is false, because if the state of hamburger1 changes, the state of hamburger2 not necessarily change and vice versa.
如果你和一个朋友同时得到一个汉堡,这是你的和他的,那么你必须决定把汉堡分成两部分,因为你gethamburger () == friend. gethamburger()为真,如果这种情况发生:friend. eathamburger(),那么你的汉堡也会被吃掉。
我可以写关于Equals和==的其他细微差别,但我有点饿了,所以我得走了。
此致敬意
拉约什·阿帕德。