clone学习

主要借助地址:http://blog.csdn.net/edward_qing_lee/article/details/8249102 的学习

引用文章中的话:除了在函数传值的时候是”引用传递”,在任何用”=”向对象变量赋值的时候都是”引用传递”。

import java.util.Enumeration;
import java.util.Hashtable;
public class practicewindow{
    public static void main(String args[])
    {
        Hashtable<String , StringBuffer> ht = new Hashtable<String,StringBuffer>();
        StringBuffer sb = new StringBuffer();
        sb.append("abc,");
        ht.put("1", sb);
        sb.append("def,");
        ht.put("2", sb);
        sb.append("mno,");
        ht.put("3", sb);
        sb.append("xyz,");
        ht.put("4", sb);
        int numObj = 0;
        Enumeration it = ht.elements();
        while(it.hasMoreElements()){
            System.out.print("get StringBuffer" + (++numObj) + "from Hashtable:");
            System.out.println(it.nextElement());
        }
    }
}

输出结果:

get StringBuffer1from Hashtable:abc,def,mno,xyz,
get StringBuffer2from Hashtable:abc,def,mno,xyz,
get StringBuffer3from Hashtable:abc,def,mno,xyz,
get StringBuffer4from Hashtable:abc,def,mno,xyz,

Hashtable中并未有实际值,而只是引用的地址。

之后是Clone。clone必须接入cloneable接口

//浅克隆  被克隆的对象各个域都是基本类型,而不存在引用类型。

public class practicewindow implements Cloneable{
    private String state;
    private String province;
    private String city;
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    @Override
    public String toString() {
        return "practicewindow [state=" + state + ", province=" + province
                + ", city=" + city + "]";
    }

    public practicewindow(String state, String province, String city) {
        super();
        this.state = state;
        this.province = province;
        this.city = city;
    }

    @Override
    protected practicewindow clone(){
        practicewindow practice = null ; 
        try {
            practice = (practicewindow) super.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return practice;
    }
}
public class Employee implements Cloneable{
    private String name ;
    private int age;
    private practicewindow pw;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public practicewindow getPw() {
        return pw;
    }
    public void setPw(practicewindow pw) {
        this.pw = pw;
    }
    public Employee(String name, int age, practicewindow pw) {
        super();
        this.name = name;
        this.age = age;
        this.pw = pw;
    }
    @Override
    public String toString() {
        return "Employee [name=" + name + ", age=" + age + ", pw=" + pw + "]";
    }
    @Override
    protected Employee clone(){
        Employee employee = null;
        try {
            employee = (Employee) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        employee.pw = pw.clone();
        return employee;
    }

    public static void main(String [] args){
        System.out.println("克隆之前:");
        practicewindow pw = new practicewindow("中国", "吉林", "长春");  
        Employee employee1 = new Employee("明日科技", 12, pw); 
        System.out.println("员工1信息:" + employee1 );  

        Employee employee2 = employee1.clone();
        employee2.getPw().setState("中国");
        employee2.getPw().setState("四川");
        employee2.getPw().setState("成都");

        System.out.println("克隆后:");
        System.out.println("员工2信息:" + employee2);  
        System.out.println("员工1信息:" + employee1);  

    }

}
克隆之前:
员工1信息:Employee [name=明日科技, age=12, pw=practicewindow [state=中国, province=吉林, city=长春]]
克隆后:
员工2信息:Employee [name=明日科技, age=12, pw=practicewindow [state=成都, province=吉林, city=长春]]
员工1信息:Employee [name=明日科技, age=12, pw=practicewindow [state=中国, province=吉林, city=长春]]

以上为深克隆。即是在克隆时对引用域practicewindow也进行了一次克隆。

class CloneC implements Cloneable{
    public String str;
    public StringBuffer strBuff;
    @Override
    protected Object clone(){
        CloneC cc = null;
        try {
            cc = (CloneC) super.clone();//克隆自己
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        return cc;
    }



}
//浅克隆。其实是同一个引用对象。
public class StrClone {
    public static void main(String[] a)
    {
        CloneC c1 = new CloneC();//初始化一个clonec
        c1.str = new String("初始化String类型");
        c1.strBuff = new StringBuffer("初始化StringBuffer");
        System.out.println("克隆前,c1.str= "+c1.str);
        System.out.println("克隆前,c1.strBuff = " + c1.strBuff);

        CloneC c2 = (CloneC)c1.clone();
        c2.str = c2.str.substring(0,5);//此处,生成一个新的对象  substring() 方法用于提取字符串中介于两个指定下标之间的字符。
        c2.strBuff = c2.strBuff.append("change strBuff clone");
        System.out.println("________________________");
        System.out.println("克隆后,c1.str= "+c1.str);
        System.out.println("克隆后,c1.strBuff = " + c1.strBuff);
        System.out.println("________________________");

        System.out.println("克隆后,c2.str= "+c2.str);
        System.out.println("克隆后,c2.strBuff = " + c2.strBuff);
    }
}

克隆前,c1.str= 初始化String类型
克隆前,c1.strBuff = 初始化StringBuffer
________________________
克隆后,c1.str= 初始化String类型
克隆后,c1.strBuff = 初始化StringBufferchange strBuff clone
________________________
克隆后,c2.str= 初始化St
克隆后,c2.strBuff = 初始化StringBufferchange strBuff clone
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值