1 /** 2 * Object通用方法:equals()、hashCode()、toString()、clone() 3 */ 4 @Data 5 @NoArgsConstructor 6 @AllArgsConstructor 7 public class objectMethodExample { 8 9 private int x; 10 private int y; 11 private int z; 12 13 @Override 14 public boolean equals(Object o){ 15 if (this == o) return true; 16 if (o == null || getClass()!= o.getClass()) return false; 17 objectMethodExample objectMethodExample = (objectMethodExample) o; 18 19 if (x!=objectMethodExample.x) return false; 20 if (y!=objectMethodExample.y) return false; 21 return z == objectMethodExample.z; 22 } 23 24 @Override 25 public int hashCode(){ 26 int result = 17; 27 result = 31 * result + x; 28 result = 31 * result + y; 29 result = 31 * result + z; 30 return result; 31 } 32 33 /** equals 34 * 对于基本类型,==判断两个值是否相等,基本类型没有equals()方法 35 * 对于引用类型,==判断两个变量是否引用同一个对象,而equals()判断引用的对象是否等价 36 */ 37 public void m1_equals(){ 38 Integer x = new Integer(1); 39 Integer y = new Integer(1); 40 System.out.println(x.equals(y)); // true 41 System.out.println(x == y); // false 42 43 } 44 45 /** hashCode 46 * 等价的两个对象hashCode一定相等,hashCode相等的两个对象不一定等价 47 * 覆盖 equals() 方法时应当总是覆盖 hashCode() 方法,保证等价的两个对象散列值也相等 48 */ 49 public void m2(){ 50 objectMethodExample o1 = new objectMethodExample(1,2,3); 51 objectMethodExample o2 = new objectMethodExample(1,2,3); 52 System.out.println(o1.equals(o2)); // true 53 54 HashSet<objectMethodExample> set = new HashSet<>(); 55 set.add(o1); 56 set.add(o2); 57 // 重写hashCode后这两个等价的对象被当作是一样的 58 System.out.println(set.size()); 59 60 } 61 62 public static void main(String[] args) { 63 objectMethodExample objectMethodExample = new objectMethodExample(); 64 objectMethodExample.m2(); 65 66 } 67 68 } 69 70 /** Object-clone。clone是Object的一个protected方法 71 * Cloneable接口规定:如果一个类没有实现Cloneable又调用了clone()方法就会抛出CloneNotSupportedException 72 */ 73 class cloneExample implements Cloneable{ 74 75 @Override 76 protected Object clone() throws CloneNotSupportedException { 77 return super.clone(); 78 } 79 80 public static void main(String[] args) throws CloneNotSupportedException { 81 cloneExample cloneExample = new cloneExample(); 82 cloneExample c2 = (cloneExample) cloneExample.clone(); 83 } 84 85 } 86 87 /** Object-clone,浅拷贝、深拷贝 88 */ 89 class shallowCloneExample implements Cloneable{ 90 private int[] arr; 91 92 public void set(int index,int value){ 93 arr[index] = value; 94 } 95 96 public int get(int index){ 97 return arr[index]; 98 } 99 100 // 浅拷贝:拷贝对象和原始对象的引用类型引用同一个对象 101 // @Override 102 // public shallowCloneExample clone() throws CloneNotSupportedException { 103 // return (shallowCloneExample) super.clone(); 104 // } 105 106 // 深拷贝:拷贝对象和原始对象的引用类型引用不同对象 107 @Override 108 public shallowCloneExample clone() throws CloneNotSupportedException { 109 shallowCloneExample result = (shallowCloneExample) super.clone(); 110 // 新建一个arr用来拷贝原始arr的数据 111 result.arr = new int[arr.length]; 112 System.arraycopy(arr, 0, result.arr, 0, arr.length); 113 return result; 114 } 115 116 public shallowCloneExample(){ 117 arr = new int[10]; 118 for (int i=0;i<arr.length;i++){ 119 arr[i] = i; 120 } 121 } 122 123 public static void main(String[] args) { 124 shallowCloneExample s1 = new shallowCloneExample(); 125 shallowCloneExample s2 = new shallowCloneExample(); 126 try { 127 s2 = s1.clone(); 128 } catch (CloneNotSupportedException e) { 129 e.printStackTrace(); 130 } 131 s1.set(2,222); 132 System.out.println(s2.get(2)); 133 } 134 135 } 136 137 /** Object-clone 138 * 使用clone()拷贝一个对象既复杂又有风险,会抛出异常,最好不要使用clone(),可以使用拷贝构造函数或拷贝工厂 139 */ 140 class cloneConstructExample{ 141 private int[] arr; 142 143 public void set(int index,int value){ 144 arr[index] = value; 145 } 146 147 public int get(int index){ 148 return arr[index]; 149 } 150 151 public cloneConstructExample(){ 152 arr = new int[10]; 153 for (int i=0;i<arr.length;i++){ 154 arr[i] = i; 155 } 156 } 157 158 // 拷贝构造函数 159 public cloneConstructExample(cloneConstructExample source){ 160 arr = new int[source.arr.length]; 161 System.arraycopy(arr,0,source.arr,0,source.arr.length); 162 } 163 164 public static void main(String[] args) { 165 cloneConstructExample c1 = new cloneConstructExample(); 166 cloneConstructExample c2 = new cloneConstructExample(); 167 c1.set(2,222); 168 System.out.println(c2.get(2)); 169 } 170 171 }
标签:arr,objectMethodExample,int,clone,Object,equals,result,public From: https://www.cnblogs.com/jinziguang/p/17251239.html