Java基础(十七)-----Object探秘

来源:互联网 发布:昆山海隆软件待遇 编辑:程序博客网 时间:2024/05/22 08:23

所有类的超类

①Object类是所有类的最终祖先,Java中的每个类都由Object类继承而来,但在定义类的时候,不需要显式说明该类继承自Object。所有的对象都可以写成:  Object o = new aClass();。
②除了基本数据类型,Java中所有的都是对象,包括我们常见的一个方法,一个变量,参数都可以表示成一个对象。

equals方法

①Object类中的equals方法用来检测一个对象是否等于另外一个对象,在Object类中,这个访法用于判断两个引用是否指向同一个对象。对于大多数具有实际意义的类来说:比较两个引用是否指向同一个对象没有太大的意义。有的时候需要比较两个引用对象中的域是否相等,比如说:一个公民类,如果两个公民的ID一样,那么它们就表示同一个人。所以,很多类都需要改写equals方法。
根据Java规范equals方法应该满足的条件:
          ①自反性:x.equlas(y)返回true,那么y.equlas(x)也应该返回true.
          ②对称性:当且仅当x.equlas(y)返回true,y.equlas(x)才能返回true。
          ③传递性:如果x.equlas(y)=true,y.equlas(z)=true,那么x.equlas(z)也应该返回true
          ④一致性:如果x和y没有变化,那么反复调用x.equals(y)返回值不变
          ⑤x.equla(null)应该返回false.
③重载equals方法的步骤:
          ①:显示命名为otherObject,然后将otherObject转化为other变量。
          ②:检测this与otherObject是否引用同一个对象。
                        this==otherObject
          ③:检测otherObject是否为null,如果为空,返回false
          ④:检测this与otherObject是否属于同一个类:
                       ①如果只是比较同类那么可以使用x.getClass()方法。[因为很满足自反性]
                       ②如果需要子类父类相比较:使用 instanceof运算符。[可以满足子类与父类的比较]
          ⑤将otherObject类转化为此类
          ⑥比较两个对象的域是否相等。

hashcode方法

hashcode方法可以说与equlas方法形影不离。Object类的hashcode方法会返回该对象的地址。当我你们在改写equlas方法的时候,就需要改写hashcode方法,以便用户可以将对象插入到散列表中去。
Java编程规范规定:如果两个对象的equlas方法返回true,那么它们的hashcode方法返回的数值也应该改相等。
代码示例:
package com.object.test;import java.util.Date;public class Employee {int id;String name;int age;double salary;String position;Date birthday;Employee(){super();}Employee(int id,String name,int age,double salary,String positon,Date birhtday){this.id=id;this.name=name;this.age=age;this.salary=salary;this.position=positon;this.birthday=birhtday;}public int getId() {return id;}public void setId(int id) {this.id = id;}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 double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public String getPosition() {return position;}public void setPosition(String position) {this.position = position;}@Override public boolean equals(Object otherObject){//检测otherObject是否是空引用if(otherObject==null){return false;}//检查this和otherObject是否引用同一个对象那个if(this==otherObject){return true;}//检测otherObject是否是Employee类的对象或者是子类对象。if(!(otherObject instanceof Employee)){return false;}/*if(!(this.getClass()==otherObject.getClass())){return false;}*/Employee e =(Employee)otherObject;if(this.id==e.id&&this.name==e.name&&this.age==e.age&&this.salary==e.salary&&this.position==e.position&&this.birthday.equals(e.birthday)){return true;}else{return false;}}@Overridepublic int hashCode() {return (int) (1*this.id+2*this.age+3*this.salary);}}class Manager extends Employee{double bonus;Manager(int id,String name,int age,double salary,String position,Date birthday){super(id,name,age,salary,position,birthday);}Manager(int id,String name,int age,double salary,String position,Date birthday,double bonus){super(id,name,age,salary,position,birthday);this.bonus=bonus;}@Override public boolean equals(Object otherObject){if(!(super.equals(otherObject))){return false;}else{Manager m =(Manager)otherObject;return this.bonus==m.bonus;}}@Overridepublic int hashCode() {returnsuper.hashCode()+4*(int)bonus;}}
package com.object.test;import java.util.Date;public class TestCalss {public static void main(String[] args) {Employee em1= new Employee(1,"xiaowang",23,3000.0,"A",new Date(10000));Employee em2= new Employee(1,"xiaowang",23,3000.0,"A",new Date(10000));Manager m1 =new Manager(1,"xiaowang",23,3000.0,"A",new Date(10000));Manager m2 =new Manager(1,"xiaowang",23,3000.0,"A",new Date(10000));System.out.println(em1.equals(em2));System.out.println(m1.equals(m2));System.out.println(em1.equals(m1));System.out.println(em1.hashCode()==em2.hashCode());System.out.println(em1.hashCode()==m1.hashCode());}}



1 0
原创粉丝点击