【发布时间】:2018-02-03 15:02:13
【问题描述】:
您好,我想比较两个对象是否具有相同的属性 我创建了 John 和 John2 ,将 John 与 John 2 进行比较是错误的,而将 John 与 John 本人进行比较是正确的,为什么会这样?
public class Test {
public static void main(String[] args) {
Student John = new Student("John", 15);
Student John2 = new Student("John", 15);
System.out.println(John.equals(John2)); // FALSE why ?
System.out.println(John.equals(John)); // TRUE
}
}
.
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public boolean egal(Student c) {
return ((this.name).equals(c.name) && (this.age) == (c.age));
}
}
【问题讨论】:
标签: java object attributes boolean