| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
//equals()方法覆写 
class Person 
{ 
        private String name; 
        private int age; 
        public Person(String name,int age) 
        { 
                this.name=name; 
                this.age=age; 
        } 
        public void setName(String name) 
        { 
                this.name=name; 
        } 
        public void setAge(int age) 
        { 
                this.age=age; 
        } 
        public String getName() 
        { 
                return this.name; 
        } 
        public int getAge() 
        { 
                return this.age; 
        } 
        public String toString() 
        { 
                return "姓名-->"+this.name+"年龄-->"+this.age; 
        } 
        public boolean equals(Object obj) 
        { 
                if(!(obj instanceof Person)) 
                { 
                        return false; 
                } 
                if(obj==null) 
                { 
                        return false; 
                } 
                if(this==obj) 
                { 
                        return true; 
                } 
                Person per=(Person) obj; 
                return this.name.equals(per.name) && this.age==per.age;                // 在equals()方法中再调用equals方法? 
        } 
} 
public class JavaDemo_0909_2334 
{ 
        public static void main(String[] args) 
        { 
                Person perA=new Person("张三",22); 
                Person perB=new Person("张三",22); 
                System.out.println(perA.equals(perB)); 
        } 
} 
 
 
 |   
 
 
 
 |