(Student类)
package object;
public class Student {
private String name;
private int age;
private char sex;
public Student(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
(main方法)
package object;
public class Test {
public static void main(String[] args) {
Student s = new Student(“Bronya”,22,‘女’);
String str = s.toString();
System.out.println(str);//默认显示地址
System.out.println(s);//让子类重写来返回内容
Student s1 = new Student(“Bronya”,22,‘女’);
Student s2 = new Student(“Bronya”,22,‘女’);
//默认比较当前对象和另一个对象地址是否相同
//父类equals存在的意义是为了让被子类重写,以便子类自己来定制比较规则
System.out.println(s1.equals(s2));//false
System.out.println(s1 == s2);//false
}
}