码迷,mamicode.com
首页 > 其他好文 > 详细

class.equals

时间:2016-12-08 02:58:49      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:运算符   nts   实例   most   bsp   pos   public   数据   val   

public boolean equals(Object obj) 
1.自身和自身应该是equals的. X.equals(x) return ture
2.如果A等于B, 那么B也就等于A . X.equals(y) 那么 Y.equals(X)
3.传递性: X.equals(Y) Y.equals(Z) 那么 X.equals(Z).
4.永恒性. 始终是相等的. 今天是相等的那么明天也会是相等的.
5.对于任何的non-null数值和null值进行比较得到的结果永远是false. X.equals(null) 输出false.\


默认实现:当且仅当X.Y都指向同一个对象时.编译器才返回ture.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y,
this method returns true if and only if x and y refer to the same object (x == y has the value true).

从上文知: x = y; 和 x.equals(y);是一样的意思

equals的东西需要自己去改写. 通过复制粘贴改写equals.

instanceof:(运算符)二进制操作符 它的作用是判断其左边对象是否为其右边类的实例,返回boolean类型的数据。

Cat C = (cat)obj; 将obj强制转换成cat类型.

J2SDK提供的一些类.如string.Date等重写了equals方法.String本身对equals进行了重写. 只要字符序列一样就会输出true.

class.equals

标签:运算符   nts   实例   most   bsp   pos   public   数据   val   

原文地址:http://www.cnblogs.com/dwj-ngu/p/6143289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!