zoukankan      html  css  js  c++  java
  • 10、匿名内部类、枚举类、日期、Math、Random、String、equals、StringBuffer、包装类、对象数组、克隆,标准输出3

    1对象的克隆(clone)

      单纯的同类的两个对象a0 a00,a0=a00只是栈指向同一个堆,而不是开辟两个新堆,修改其中一个,另一个也会受牵连。

      需要重写Clone()方法,并且实现Cloneable接口。  

      浅层克隆:仅仅对对象成员的逐成员克隆

        

    package homework.B1.src.com.C12;
    
    /**
     * Created by Administrator on 2018/2/27 0027.
     * 作业:浅层克隆:
     */
    public class A {
        public static void main(String[] args) {
            A0 a0=new A0();
            a0.setI(2);
            try {
                A0 a00=(A0)a0.clone();
                System.out.println(a00.getI());
                a0.setI(3);
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
    
        }
    }
    class A0 implements Cloneable{
        private int i;
        private String s;
    
        public A0(int i, String s) {
            this.i = i;
            this.s = s;
        }
    
        public A0() {
        }
    
        public int getI() {
            return i;
        }
    
        public void setI(int i) {
            this.i = i;
        }
    
        public String getS() {
            return s;
        }
    
        public void setS(String s) {
            this.s = s;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

      深层克隆:不仅拷贝对象的字段,而且还对对象通过字段关联的其他对象实施拷贝,即对于当前对象关联的其他对象,深克隆要先创建这个其他对象再实施克隆,迭代。

    package homework.B1.src.com.C12;
    
    /**
     * Created by Administrator on 2018/2/7 0027.
     * 深克隆
     */
    public class B {
        public static void main(String[] args) {
                Cat c=new Cat();
                Cat c1;
                c.setAnimal(new Animal());
                c.getAnimal().setName("asd");
            try {
                c1=(Cat)c.clone();
                c.getAnimal().setName("zxc");
                System.out.println(c1.getAnimal().getName());
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
    
        }
    }
    
    
    class Cat implements Cloneable{
        private Animal animal;
        private String name;
        private int age;
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            Animal animal1=(Animal)this.animal.clone();
            Cat cat1=(Cat)super.clone();
            cat1.setAnimal(animal1);
            return cat1;
        }
    
        public Cat() {
        }
    
        public Cat(Animal animal, String name, int age) {
            this.animal = animal;
            this.name = name;
            this.age = age;
        }
    
        public Animal getAnimal() {
            return animal;
        }
    
        public void setAnimal(Animal animal) {
            this.animal = animal;
        }
    
        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;
        }
    }
    class Animal implements Cloneable{
        private String name;
    
        public Animal(String name) {
            this.name = name;
        }
    
        public Animal() {
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

    应用:有些对象创建复杂,因此使用clone来进行对象创建,实现了原型模式;

  • 相关阅读:
    WEBSHELL跳板REDUH使用说明
    lcx.exe内网转发命令教程 + LCX免杀下载
    程序只启动一个实例的几种方法
    VS2010中遇到_WIN32_WINNT not defined
    VC编译错误: Nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12已经在dllmain.obj 中定义
    python(31) enumerate 的用法
    利用余弦定理计算文本的相似度
    Linux命令(24) :sort
    python(30) 获取网页返回的状态码,状态码对应问题查询
    python(29)强大的zip函数
  • 原文地址:https://www.cnblogs.com/television/p/8413925.html
Copyright © 2011-2022 走看看