/**
* java.lang.Object类是默认被每一个类继承的
*/
class Student{
private String name;
private int age;
private int gender;
public Student(){
super();
}
public void setName(String name){
this.name = name ;
}
public void setAge(int age){
this.age = age ;
}
public void setGender(int age){
this.gender = gender;
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
public int getGender(){
return gender;
}
/**
*重写Object的equals方法时,最好重要hash值
*/
public int hashCode(){
return 1 ;
}
}
class ObjectDemo{
public static void main(String []args){
Student s = new Student();
Student s1 = new Student();
//直接打印对象时默认的在调用父类继承下来的toString
System.out.println(s);
System.out.println(s1);
//判断对象是否相等不能使用“==”,因为“==”判断的是对象地址,二个对象的地址是永远不可能相同的
System.out.println(s==s1);
int hashcode = s.hashCode();
System.out.println(hashcode);
//判断两个对象是否相等一般使用equals方法
boolean isEquals = s.equals(s1);
System.out.println(isEquals);
System.out.println("学生和学生1是否相等: "+s.equals(s1));
}
}