== equal clone() Cloneable 原型模型

相等及拷贝

  • ==
  • equal
  • clone()
  • Cloneable
  • 原型模型

一 ==

==是比较地址,若两个对象用==连接返回true,那他们肯定是同一个对象;

二 equal

equal默认比较的是地址,但是任何函数都可以重载equal()函数,最著名的重载是String.equal(),String将地址比较重载为值比较。

三 clone() protected

 x.clone() != x

  • will be true, and that the expression:
     x.clone().getClass() == x.getClass()
    will be true, but these are not absolute requirements. While it is typically the case that:
     x.clone().equals(x)
    will be true, this is not an absolute requirement. 
        在使用Clone()时,必须要实现Cloneable接口,还必须重载clone()函数

四 cloneable

Java的cloneable是Java对象的一种拷贝技术,类似于serializable。在Java中,所有的对象都是存储与堆中的,cloneable就是在堆中新建一个与原来对象完全相同的对象。Java中实现cloneable有以下几个步骤:

一、要进行clone的类必须实现Cloneable接口

二、如果有必要,重写Object类的clone()方法。

三、在重写的clone()方法中,注意类所引用对象的深拷贝与浅拷贝问题。

示例代码:

[java]  view plain copy
  1. package com.example.cloneable;  
  2.   
  3. public class User implements Cloneable {  
  4.     /** 
  5.      * User id 
  6.      */  
  7.     private int id;  
  8.   
  9.     /** 
  10.      * User name 
  11.      */  
  12.     private String name;  
  13.   
  14.     /** 
  15.      * User age 
  16.      */  
  17.     private int age;  
  18.   
  19.     public int getId() {  
  20.         return id;  
  21.     }  
  22.   
  23.     public void setId(int id) {  
  24.         this.id = id;  
  25.     }  
  26.   
  27.     public String getName() {  
  28.         return name;  
  29.     }  
  30.   
  31.     public void setName(String name) {  
  32.         this.name = name;  
  33.     }  
  34.   
  35.     public int getAge() {  
  36.         return age;  
  37.     }  
  38.   
  39.     public void setAge(int age) {  
  40.         this.age = age;  
  41.     }  
  42.   
  43.     @Override  
  44.     protected Object clone() throws CloneNotSupportedException {  
  45.         return super.clone();  
  46.     }  
  47. }  


[java]  view plain copy
  1. package com.example.cloneable;  
  2.   
  3. public class Administrator implements Cloneable {  
  4.     private User user;  
  5.   
  6.     /** 
  7.      * @param args 
  8.      * @throws CloneNotSupportedException  
  9.      */  
  10.     public static void main(String[] args) throws CloneNotSupportedException {  
  11.         Administrator administrator = new Administrator();  
  12.         administrator.setUser(new User());  
  13.           
  14.         Administrator administrator2 = (Administrator)administrator.clone();  
  15.         System.out.println(administrator == administrator2);<span style="white-space:pre">          </span> #false  
  16.         System.out.println(administrator.getUser() == administrator2.getUser()); #false  
  17.     }  
  18.   
  19.     public void setUser(final User user) {  
  20.         this.user = user;  
  21.     }  
  22.   
  23.     public User getUser() {  
  24.         return this.user;  
  25.     }  
  26.   
  27.     @Override  
  28.     protected Object clone() throws CloneNotSupportedException {  
  29.         Administrator administrator = (Administrator)super.clone();  
  30.         administrator.user = (User)this.user.clone();  //此处为深拷贝
  31.         return administrator;  
  32.     }  

五 原型模型



 为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。

在派生类中覆盖基类的clone()方法,并声明为public。切记clone()返回为Object类型,需要进行强制转换。

在派生类的clone()方法中,调用super.clone()。

在派生类中实现Cloneable接口。Cloneable接口没有定义任何抽象方法,实现该接口只是通知系统该对象支持clone,这样的接口我们称之为“标识接口”。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值