Java中clone方法使用笔记

作者:吃橘子的Crow 时间:2023-03-26 07:44:38 

注解

定义: 注解是一种注释机制,它可以注释包、类、方法、变量、参数,在编译器生成类文件时,标注可以被嵌入到字节码中。

注解的分类:

内置注解

Override :重写方法,引用时没有该方法时会编译错误

public class Animals {
   public void run(){
       System.out.println("动物跑");
   }
}
public class Cat extends Animals{
   @Override
   public void run1() {
       super.run();
   }
}

Java中clone方法使用笔记

Deprecated :标记过时方法,会造成编译警告

public class Animals {
   @Deprecated
   public void run(){
       System.out.println("动物跑");
   }
}

Java中clone方法使用笔记

SuppressWarnings :用于编译器去忽略注解中的声明报告

FunctionalInterface :用于指示被修饰的接口是函数式接口

元注解(修饰注解的注解)

@Retention -标记这个注解存储在哪里

@Documented -标记这些注解是否包含在用户文档中

@Target -标记这些注解时java哪种成员

public enum ElementType {
   /** Class, interface (including annotation type), or enum declaration */
   //可以应用于类的任何元素
   TYPE,

//可以用于字段或属性
   /** Field declaration (includes enum constants) */
   FIELD,

//可以用于方法级注释
   /** Method declaration */
   METHOD,

//可以用于方法的参数
   /** Formal parameter declaration */
   PARAMETER,

//可以应用于构造函数
   /** Constructor declaration */
   CONSTRUCTOR,

//可以用于局部变量
   /** Local variable declaration */
   LOCAL_VARIABLE,

/** Annotation type declaration */
   ANNOTATION_TYPE,

//可以用于包声明
   /** Package declaration */
   PACKAGE,

/**
    * Type parameter declaration
    *
    * @since 1.8
    */
   TYPE_PARAMETER,

/**
    * Use of a type
    *
    * @since 1.8
    */
   TYPE_USE
}

@Inherited -标记这个注解时继承于哪个类

@Repeatable -标识某注解可以在同一个声明上使用多次

public enum RetentionPolicy {
   /**
    * Annotations are to be discarded by the compiler.
    */
   SOURCE,//在源文件中有效(源文件保存)

/**
    * Annotations are to be recorded in the class file by the compiler
    * but need not be retained by the VM at run time.  This is the default
    * behavior.
    */
   CLASS,//在class文件中有效(class保存)

/**
    * Annotations are to be recorded in the class file by the compiler and
    * retained by the VM at run time, so they may be read reflectively.
    *
    * @see java.lang.reflect.AnnotatedElement
    */
   RUNTIME//在运行时有效(运行时保留)
}

自定义注解

注解类:

@Target(ElementType.FIELD)//作用在类的属性上
@Retention(RetentionPolicy.RUNTIME)//运行时生效
public @interface NotNull {

String message() default "";

int length() default 0;

String lengthmessage() default "";
}

model类:

public class User {

private int num;

@NotNull(message="姓名不能为空",length=3,lengthmessage="长度不能小于3")
   private String name;

public String getName() {
       return name;
   }

public void setName(String name) {
       this.name = name;
   }

public int getNum() {
       return num;
   }

public void setNum(int num) {
       this.num = num;
   }

}

测试代码:

public class Test {

public static void main(String[] args) throws NoSuchMethodException, SecurityException, Exception {
           User user=new User();
         Field[] fields=user.getClass().getDeclaredFields();//将类中的字段存储在field数组中
         //对数组中的字段进行强循环
         for(Field filed:fields){
             NotNull notNull=filed.getAnnotation(NotNull.class);//获取注释类型
             if(notNull!=null){
   Method method = user.getClass().getMethod("get" + getMethodName(filed.getName()));//获取方法对象
          Object value = method.invoke(user);//调用类的实例对象
            if(value==null){
          System.err.println(filed.getName()+notNull.message());//打印输出相应的字段和注释信息
                     throw new NullPointerException(notNull.message());//抛出异常信息
                 }
                 else if(String.valueOf(value).length()< notNull.length()){//判断字符串长度
                     System.err.println(filed.getName()+notNull.lengthmessage());

}
             }
         }

}

/**
        * 把一个字符串的第一个字母大写
        */
       private static String getMethodName(String fildeName) throws Exception {
           byte[] items = fildeName.getBytes();
           items[0] = (byte) ((char) items[0] - 'a' + 'A');
           return new String(items);
       }
}

对象克隆

原因:new出来的对象属性都是初始化的值,不能保存当前对象&ldquo;状态&rdquo;,clone解决了这个问题

//这种形式的代码复制的是引用,即对象在内存中的地址,car1和car2指向同一个对象
Car car1=new Car();
Car car2=car1;

如何实现克隆

克隆分为浅克隆和深克隆,下面就简单的介绍它们之前的区别:

  • 浅克隆(值类型克隆值,引用类型传递地址)

model类:

public class Person implements  Cloneable{

int num;
    String name;
    Address address;

public Person() {
   }

public Person(int num, String name) {
       this.num = num;
       this.name = name;
   }

public int getNum() {
       return num;
   }

public void setNum(int num) {
       this.num = num;
   }

public String getName() {
       return name;
   }

public void setName(String name) {
       this.name = name;
   }

public Address getAddress() {
       return address;
   }

public void setAddress(Address address) {
       this.address = address;
   }

@Override
   protected Person clone() throws CloneNotSupportedException {
       Person person = (Person)super.clone();
      // person.address = (Address)address.clone();   //深度复制  联同person中关联的对象也一同克隆.
       return person;
   }
   @Override
   public String toString() {
       return "Person{" +
               "num=" + num +
               ", name='" + name + '\'' +
               ", address=" + address +
               '}';
   }
}

引用类:

public class Address {

String  address;

public String getAddress() {
       return address;
   }

public void setAddress(String address) {
       this.address = address;
   }

@Override
   public String toString() {
       return "Address{" +
               "address='" + address + '\'' +
               '}';
   }

@Override
   protected Address clone() throws CloneNotSupportedException {
       return (Address)super.clone();
   }
}

测试类:

public class Test {

public static void main(String[] args) throws CloneNotSupportedException {

Address address = new Address();
               address.setAddress("汉中");

Person p1 = new  Person(100,"jim");
              p1.setAddress(address);

Person p2 =p1.clone();
              p2.setName("tom");
              address.setAddress("西安");//
       System.out.println(p1);
   }
}

Java中clone方法使用笔记

Java中clone方法使用笔记

Java中clone方法使用笔记

浅克隆中引用对象进行的是引用地址传递,原引用对象和克隆对象指向同一个引用地址

强克隆(值类型克隆值,引用类型克隆一个带有原数据的新的地址)

Java中clone方法使用笔记

引用类:

public class Address implements Cloneable{

String  address;

public String getAddress() {
       return address;
   }

public void setAddress(String address) {
       this.address = address;
   }

@Override
   public String toString() {
       return "Address{" +
               "address='" + address + '\'' +
               '}';
   }

@Override
   protected Address clone() throws CloneNotSupportedException {
       return (Address)super.clone();
   }
}

model类:

public class Person implements  Cloneable{

int num;
    String name;
    Address address;

public Person() {
   }

public Person(int num, String name) {
       this.num = num;
       this.name = name;
   }

public int getNum() {
       return num;
   }

public void setNum(int num) {
       this.num = num;
   }

public String getName() {
       return name;
   }

public void setName(String name) {
       this.name = name;
   }

public Address getAddress() {
       return address;
   }

public void setAddress(Address address) {
       this.address = address;
   }

@Override
   protected Person clone() throws CloneNotSupportedException {
       Person person = (Person)super.clone();
       person.address = (Address)address.clone();   //深度复制  联同person中关联的对象也一同克隆.
       return person;
   }

@Override
   public String toString() {
       return "Person{" +
               "num=" + num +
               ", name='" + name + '\'' +
               ", address=" + address +
               '}';
   }
}

测试:

public class Test {

public static void main(String[] args) throws CloneNotSupportedException {

Address address = new Address();
               address.setAddress("汉中");

Person p1 = new  Person(100,"jim");
              p1.setAddress(address);

Person p2 =p1.clone();
              p2.setName("tom");
              address.setAddress("西安");
       System.out.println(p1);
       System.out.println(p2);
   }
}

Java中clone方法使用笔记

强克隆中的引用类型新创建的地址赋给克隆对象引用类型

我们也可以通过序列化的方式对对象进行克隆,代码如下:

引用类:

public class Address  implements Serializable {

String  address;

public String getAddress() {
       return address;
   }

public void setAddress(String address) {
       this.address = address;
   }

@Override
   public String toString() {
       return "Address{" +
               "address='" + address + '\'' +
               '}';
   }

}

model类:


public class Person implements Serializable {

int num;
    String name;
    Address address;

public Person() {
   }

public Person(int num, String name) {
       this.num = num;
       this.name = name;
   }

public int getNum() {
       return num;
   }

public void setNum(int num) {
       this.num = num;
   }

public String getName() {
       return name;
   }

public void setName(String name) {
       this.name = name;
   }

public Address getAddress() {
       return address;
   }

public void setAddress(Address address) {
       this.address = address;
   }

/**
    * 自定义克隆方法
    * @return
    */
   public Person myclone() {
           Person person = null;
             try { // 将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                     ObjectOutputStream oos = new ObjectOutputStream(baos);
                     oos.writeObject(this);
           // 将流序列化成对象
                   ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                    ObjectInputStream ois = new ObjectInputStream(bais);
                    person = (Person) ois.readObject();
                 } catch (IOException e) {
                    e.printStackTrace();
                 } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            return person;
         }

@Override
   public String toString() {
       return "Person{" +
               "num=" + num +
               ", name='" + name + '\'' +
               ", address=" + address +
               '}';
   }
}

测试类:

public class Test {

public static void main(String[] args) throws CloneNotSupportedException {

Address address = new Address();
               address.setAddress("汉中");

Person p1 = new  Person(100,"jim");
       p1.setAddress(address);

Person p2 =p1.myclone();
              p2.setName("tom");
              address.setAddress("西安");

System.out.println(p1);
       System.out.println(p2);

}
}

来源:https://blog.csdn.net/dfdbb6b/article/details/128959163

标签:java,clone方法
0
投稿

猜你喜欢

  • java数据结构与算法之noDups去除重复项算法示例

    2023-06-19 08:50:34
  • 彻底掌握C语言strcpy函数的用法

    2023-07-03 07:21:18
  • 简单了解Android性能优化方向及相关工具

    2022-10-15 19:10:54
  • Android studio 3.0安装配置方法图文教程

    2021-05-26 14:48:42
  • C# 三种序列化方法分享

    2022-09-21 03:43:05
  • WPF自定义控件和样式之自定义按钮(Button)

    2022-04-09 14:39:32
  • C#语言中条件与&&与条件或||的区别

    2022-10-06 22:51:55
  • C#获取上个月第一天和最后一天日期的方法

    2023-02-22 07:33:44
  • c#获取gridview的值代码分享

    2023-06-27 22:23:50
  • Android Usb设备的监听(Dev)外设端口的判定以及耳机的插拔

    2022-12-07 19:23:44
  • Android界面设计(APP设计趋势 左侧隐藏菜单右边显示content)

    2023-05-20 04:54:24
  • Kotlin数据容器深入讲解

    2022-03-28 05:19:34
  • Android实现Flip翻转动画效果

    2022-05-11 20:08:57
  • C# 使用SharpZipLib生成压缩包的实例代码

    2021-08-29 20:32:57
  • android实现点击按钮控制图片切换

    2022-10-16 02:38:55
  • java的url方式、本地方式获取json文件内容

    2023-08-22 18:30:23
  • Java创建多线程的两种方式对比

    2023-09-14 21:02:49
  • 解析Tars-Java客户端源码

    2023-04-08 01:18:39
  • Java 队列实现原理及简单实现代码

    2021-07-19 05:12:56
  • 如何使用Java redis实现发送手机验证码功能

    2023-11-26 17:25:00
  • asp之家 软件编程 m.aspxhome.com