Object类的方法学习

xiaoxiao2021-02-27  186

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

转载请注明原文地址: https://www.6miu.com/read-11633.html

最新回复(0)