重写equals/clone

来源:互联网 发布:软件产品设计文档模板 编辑:程序博客网 时间:2024/05/20 07:34

Equals 方法通常是用来判断两个对象是否相等。Set集合通常要依赖这个方法,如果一个类的对象若要存入set集合,通常要重写这个方法

一般重写了equals方法,就要重写hashcode方法

public class Student implements Cloneable{private String id;//如果两个对象的id值相同,说明这个两个对象是同一个学生private String name;@Overridepublic Student clone() throws CloneNotSupportedException {// TODO Auto-generated method stubreturn (Student) super.clone();}@Overridepublic boolean equals(Object obj) {
<span style="white-space:pre"></span><pre name="code" class="java">if(obj == this)return true;

if(obj==null)return false;if(obj instanceof Student){//是这个类的实例Student s = (Student) obj;if(s.id!=null&&s.id.equals(this.id)){//按业务规则来判断是否为同一个对象return true;}else{return false;}}else{//不是这个类的实例return false;}}/* * 一般重写了equals方法,就要重写hashCode方法 */@Overridepublic int hashCode() {return this.id.hashCode();}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}

测试

import org.junit.Test;public class TestStudent {@Testpublic void test() throws CloneNotSupportedException{Student s = new Student();//第一次创建原生的对象s.setId("niit01");Student s2 = s.clone();//产生新的对象,与原生对象地址不同System.out.println(s.equals(s2));}}

克隆:

一个类要重写clone方法

1.声明实现Cloneable接口

2.重写object类中的clone()方法,将protected改成public,重写clone方法时,在重写方法的内部要调用super.clone()对象的clone分成

1).浅克隆(浅复制,浅拷贝)

2)深克隆(...

 

深克隆与浅克隆的区别在于引用类型的属性的处理

浅克隆

public class Computer{private String brand;public Computer() {super();// TODO Auto-generated constructor stub}public Computer(String brand) {super();this.brand = brand;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}}public class Student implements Cloneable{private String name;public Computer c;public Student(String name) {super();this.name = name;}public void setName(String name){this.name = name;}public void setComputer(Computer c){this.c = c;}public String toString(){return this.name+"\t 电脑的品牌"+c.getBrand();}public Object clone()  throws CloneNotSupportedException{return super.clone();}public static void main(String[] args) throws CloneNotSupportedException {Student stu = new Student("李三");Computer c = new Computer();c.setBrand("lenovo");stu.c = c;Student s1 = (Student) stu.clone();//创建一个对象,这个对象与stu完全相同System.out.println(stu);  //李三 电脑的品牌lenovoSystem.out.println(s1);//李三 电脑的品牌lenovoSystem.out.println(s1==stu);//这两个变量指向两个不同的对象,所以falses1.setName("李四");//将克隆对象的name属性设置为李四,对原来的对象是没有影响的System.out.println(s1);   //李四 电脑的品牌lenovoSystem.out.println(stu);//李三 电脑的品牌lenovoc.setBrand("dell");s1.setComputer(c);//修改引用数据类型System.out.println(s1);//李四 电脑的品牌dellSystem.out.println(stu);//李三 电脑的品牌dells1.setComputer(new Computer("mac"));//修改引用数据类型System.out.println(s1);//李四 电脑的品牌macSystem.out.println(stu);//李三 电脑的品牌dell}}

深克隆

public class Computer implements Cloneable{private String brand;public Computer() {super();// TODO Auto-generated constructor stub}public Computer(String brand) {super();this.brand = brand;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}@Overridepublic Computer clone() throws CloneNotSupportedException {return (Computer)super.clone();}}public class Student implements Cloneable{private String name;public Computer c;public Student(String name) {super();this.name = name;}public void setName(String name){this.name = name;}public void setComputer(Computer c){this.c = c;}public String toString(){return this.name+"\t 电脑的品牌"+c.getBrand();}public Object clone()  throws CloneNotSupportedException{Student s = (Student) super.clone();if(s.c!=null)s.c=s.c.clone();//厂商是影响类型,需要新建一个对象return s;}public static void main(String[] args) throws CloneNotSupportedException {Student stu = new Student("李三");Computer c = new Computer();c.setBrand("lenovo");stu.c = c;Student s1 = (Student) stu.clone();//创建一个对象,这个对象与stu完全相同System.out.println(stu);  //李三 电脑的品牌lenovoSystem.out.println(s1);//李三 电脑的品牌lenovoSystem.out.println(s1==stu);//这两个变量指向两个不同的对象,所以falses1.c.setBrand("zzz");System.out.println(s1);   //李四 电脑的品牌zzzSystem.out.println(stu);//李三 电脑的品牌lenovo}}


0 0
原创粉丝点击