public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) {
Person p = (Person)obj;
return this.name.equals(p.name)&& this.age == p.age;
}
}
上面代码重写equals方法,方法中,this代表当前对象的引用,也就是当前对象Person中的name,与p.name里面的字符串作比较.我不明白的地方就是对象的引用P中的name不是加了也private吗,怎么可以直接用p.调用.
|
|