zoukankan      html  css  js  c++  java
  • 对象相等性和同一性

    概念同一性:同一个引用

    相等性:所有部分都相等,但并一定是同一个引用。包含4个特征

    1、自反性,自已等于自己

    2、对称性,x=y,y也一定等于x

    3、可传递性,x=y,y=z,则x=z

    4、一致性,x,y都不变的情况下,比较结果不能变

    Object.Equals 方法

    The type of comparison between the current instance and the obj parameter depends on whether the current instance is a reference type or a value type.

    比较的第一步是判断是引用类型还是值类型

    1、If the current instance is a reference type, the M:System.Object.Equals(System.Object) method tests for reference equality, and a call to the M:System.Object.Equals(System.Object) method is equivalent to a call to the M:System.Object.ReferenceEquals(System.Object,System.Object) method. Reference equality means that the object variables that are compared refer to the same object.

    如果是引用类型,则调用System.Object.ReferenceEquals方法

    2、If the current instance is a value type, the M:System.Object.Equals(System.Object) method tests for value equality. Value equality means the following

    1)The two objects are of the same type.

    看是否是同一类型

    2)The values of the public and private fields of the two objects are equal.

    比较公有和私有字段是否相等

    以上两条都满足,则相等

  • 相关阅读:
    洛谷P2146 [NOI2015]软件包管理器
    洛谷P3038 [USACO11DEC]牧草种植Grass Planting
    洛谷P2831 愤怒的小鸟
    洛谷P1084 疫情控制
    洛谷P3258 [JLOI]2014松鼠的新家
    洛谷P1084 运输计划
    洛谷P2051 [AHOI2009]中国象棋
    洛谷P1438 无聊的数列
    洛谷P1312 Mayan游戏
    luogu P1038 神经网络
  • 原文地址:https://www.cnblogs.com/qook/p/5421044.html
Copyright © 2011-2022 走看看