在object这个类里面equals方法默认的实现是当前对象引用和你要比较的对象的引用是不是同一个对象。 equals方法的返回值是boolean j2sdk提供了一些类,如:string data等重写了equals方法例子程序:
Java代码
public class TestEquals {
public static void main(String[] args) {
Cat c1 = new Cat(1, 2, 3);
Cat c2 = new Cat(1, 2, 6);
System.out.println(c1 == c2); //false
System.out.println(c1.equals(c2)); //false
String s1 = new String("hello");
String s2 = new String("hello");
String m1 = "hello";
String m2 = "hello";
System.out.println(s1 == s2); //false
System.out.println(m1 == m2); //true
System.out.println(s1.equals(s2)); //true
}
}
//实体类
class Cat {
int color;
int height, weight;
public Cat(int color, int height, int weight) {
this.color = color;
this.height = height;
this.weight = weight;
}
//重写equals方法
public boolean equals(Object obj) {
if(obj == null) return false;
else {
if(obj instanceof Cat) {
Cat c = (Cat)obj;
if(c.color == this.color && c.height
== this.height && c.weight == this.weight) {
return true;
}
}
}
return false;
}
}更多Object类之equals方法 相关文章请关注PHP中文网!
基于ECSHOP2.7.2制作,模板使用的是早期的凡客模板。整站大气,清爽。适合综合,鞋子,服饰类商城使用。具体安装方法在程序包中有说明,在使用之前请看下。 大体方法:1.上传程序至网站根目录,访问:域名/diguo (用户名:admin 密码:123456)2.设置好数据库信息,然后恢复数据,数据目录在www.shopex5.com下.3.修改data目录下的config数据库配置文件。4.登陆









