Object类的方法学习

来源:互联网 发布:行知职高吧 编辑:程序博客网 时间:2024/06/04 19:22
Object类的方法:

boolean equals(Object obj), int hashCode()(默认与System.identityHashCode(Object x)方法计算得到的结果相同)

protected void finalize(),Class<?> getClass(),String toString(), wait(), notify(), notifyAll(), protected clone()

class Address{String detail;public Address(String detail){this.detail = detail;}}// 实现Cloneable接口class User implements Cloneable{int age;Address address;public User(int age){this.age = age;address = new Address("广州天河");}// 通过调用super.clone()来实现clone()方法public User clone()throws CloneNotSupportedException{return (User)super.clone();}}public class CloneTest{public static void main(String[] args)throws CloneNotSupportedException{User u1 = new User(29);// clone得到u1对象的副本。User u2 = u1.clone();// 判断u1、u2是否相同System.out.println(u1 == u2);      //①// 判断u1、u2的address是否相同System.out.println(u1.address == u2.address);     //②}}

false
true