标签:boolean new print 传递 getname ted stat generated 向下转型
package equalsdemo; /* * 重写object中的equals方法 */ public class equalsdemo { public static void main(String[] args) { person p1 = new person("zhangsan",54); person p2 = new person("zhangsan",54); if (p1.equals(p2)) { System.out.println("这两个对象相等!!!"); } else { System.out.println("这两个 no 对象相等!!!"); } } } class person{ private String name; private int age; public person(String name,int age){ this.name = name; this.age = age; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public String getName() { return name; } public void setName(String name) { this.name = name; } //复写equals方法 @Override public boolean equals(Object obj) { // TODO Auto-generated method stub //如果两个对象地址相等则相等 if (this==obj) { return true; } //判断两个对象的值是否相等 //把传递过来的obj向下转型 person p = (person)obj; if (p.getName().equals(this.getName())&&p.getAge()==this.getAge()) { return true; } else { return false; } } }
标签:boolean new print 传递 getname ted stat generated 向下转型
原文地址:http://www.cnblogs.com/yuanyuan2017/p/6561993.html