其他分享
首页 > 其他分享> > 软件构造博客2

软件构造博客2

作者:互联网

今天的博客有关于ADT和OOP的等价性

等价关系的定义以前是学过的:自反传递对称

在Immutable类型中,如果在AF中能够映射到同样的结果,那么二者就具有等价性

而站在外部观察者角度:对两个对象调用任何相同的操作,都会得到相同的结果,则认为这两个对象是等价 对于equals来说有一些需要注意的事项: 在自定义ADT时,需要重写Object的equals(); 对基本数据类型,使用==判定相等 对对象类型,使用equals() 对于equals() in Object:“相等”的对象,其hashCode()的结果必须 一致,不相等的对象,也可以映射为同样的hashCode,但性能会变差 对于Mutalbe种类: 观察等价性:在不改变状态的情况下,两个mutable对象是否看起来一致 行为等价性:调用对象的任何方法都展示出一致的结果 我们往往倾向于实现观察等价性 这里记录了观察等价性和行为等价性的几个例子 Date类的equals()的spec:"Two Date objects are equal if and only if the getTime method returns the same long value for both. “ 观察等价性 § List类的equals()的spec:"Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. “ – Two lists are defined to be equal if they contain the same elements in the same order. 观察等价性 StringBuilder类的equals继承自Object类 J 行为等价性 这里记录了一些要点: 对可变类型,实现行为等价性即可 只有指向同样内存空间的objects,才是相等的。 所以对可变类型来说,无需重写这两个函数,直接继承Object的两个方法即可。 如果一定要判断两个可变对象看起来是否一致,最好定义一个新的方法

标签:对象,Object,equals,等价,博客,equal,构造,same,软件
来源: https://www.cnblogs.com/fsscg/p/16300231.html