当前位置:   article > 正文

Java中的clone方法_java clone

java clone

注解

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

注解的分类

  1. 内置注解

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

  1. public class Animals {
  2. public void run(){
  3. System.out.println("动物跑");
  4. }
  5. }
  1. public class Cat extends Animals{
  2. @Override
  3. public void run1() {
  4. super.run();
  5. }
  6. }
  • Deprecated :标记过时方法,会造成编译警告

  1. public class Animals {
  2. @Deprecated
  3. public void run(){
  4. System.out.println("动物跑");
  5. }
  6. }
  • SuppressWarnings :用于编译器去忽略注解中的声明报告

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

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

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

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

  1. public enum ElementType {
  2. /** Class, interface (including annotation type), or enum declaration */
  3. //可以应用于类的任何元素
  4. TYPE,
  5. //可以用于字段或属性
  6. /** Field declaration (includes enum constants) */
  7. FIELD,
  8. //可以用于方法级注释
  9. /** Method declaration */
  10. METHOD,
  11. //可以用于方法的参数
  12. /** Formal parameter declaration */
  13. PARAMETER,
  14. //可以应用于构造函数
  15. /** Constructor declaration */
  16. CONSTRUCTOR,
  17. //可以用于局部变量
  18. /** Local variable declaration */
  19. LOCAL_VARIABLE,
  20. /** Annotation type declaration */
  21. ANNOTATION_TYPE,
  22. //可以用于包声明
  23. /** Package declaration */
  24. PACKAGE,
  25. /**
  26. * Type parameter declaration
  27. *
  28. * @since 1.8
  29. */
  30. TYPE_PARAMETER,
  31. /**
  32. * Use of a type
  33. *
  34. * @since 1.8
  35. */
  36. TYPE_USE
  37. }
  • @Inherited -标记这个注解时继承于哪个类

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

  1. public enum RetentionPolicy {
  2. /**
  3. * Annotations are to be discarded by the compiler.
  4. */
  5. SOURCE,//在源文件中有效(源文件保存)
  6. /**
  7. * Annotations are to be recorded in the class file by the compiler
  8. * but need not be retained by the VM at run time. This is the default
  9. * behavior.
  10. */
  11. CLASS,//在class文件中有效(class保存)
  12. /**
  13. * Annotations are to be recorded in the class file by the compiler and
  14. * retained by the VM at run time, so they may be read reflectively.
  15. *
  16. * @see java.lang.reflect.AnnotatedElement
  17. */
  18. RUNTIME//在运行时有效(运行时保留)
  19. }
  1. 自定义注解

注解类:

  1. @Target(ElementType.FIELD)//作用在类的属性上
  2. @Retention(RetentionPolicy.RUNTIME)//运行时生效
  3. public @interface NotNull {
  4. String message() default "";
  5. int length() default 0;
  6. String lengthmessage() default "";
  7. }

model类:

  1. public class User {
  2. private int num;
  3. @NotNull(message="姓名不能为空",length=3,lengthmessage="长度不能小于3")
  4. private String name;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public int getNum() {
  12. return num;
  13. }
  14. public void setNum(int num) {
  15. this.num = num;
  16. }
  17. }

测试代码:

  1. public class Test {
  2. public static void main(String[] args) throws NoSuchMethodException, SecurityException, Exception {
  3. User user=new User();
  4. Field[] fields=user.getClass().getDeclaredFields();//将类中的字段存储在field数组中
  5. //对数组中的字段进行强循环
  6. for(Field filed:fields){
  7. NotNull notNull=filed.getAnnotation(NotNull.class);//获取注释类型
  8. if(notNull!=null){
  9. Method method = user.getClass().getMethod("get" + getMethodName(filed.getName()));//获取方法对象
  10. Object value = method.invoke(user);//调用类的实例对象
  11. if(value==null){
  12. System.err.println(filed.getName()+notNull.message());//打印输出相应的字段和注释信息
  13. throw new NullPointerException(notNull.message());//抛出异常信息
  14. }
  15. else if(String.valueOf(value).length()< notNull.length()){//判断字符串长度
  16. System.err.println(filed.getName()+notNull.lengthmessage());
  17. }
  18. }
  19. }
  20. }
  21. /**
  22. * 把一个字符串的第一个字母大写
  23. */
  24. private static String getMethodName(String fildeName) throws Exception {
  25. byte[] items = fildeName.getBytes();
  26. items[0] = (byte) ((char) items[0] - 'a' + 'A');
  27. return new String(items);
  28. }
  29. }

对象克隆

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

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

如何实现克隆

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

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

model类:

  1. public class Person implements Cloneable{
  2. int num;
  3. String name;
  4. Address address;
  5. public Person() {
  6. }
  7. public Person(int num, String name) {
  8. this.num = num;
  9. this.name = name;
  10. }
  11. public int getNum() {
  12. return num;
  13. }
  14. public void setNum(int num) {
  15. this.num = num;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public Address getAddress() {
  24. return address;
  25. }
  26. public void setAddress(Address address) {
  27. this.address = address;
  28. }
  29. @Override
  30. protected Person clone() throws CloneNotSupportedException {
  31. Person person = (Person)super.clone();
  32. // person.address = (Address)address.clone(); //深度复制 联同person中关联的对象也一同克隆.
  33. return person;
  34. }
  35. @Override
  36. public String toString() {
  37. return "Person{" +
  38. "num=" + num +
  39. ", name='" + name + '\'' +
  40. ", address=" + address +
  41. '}';
  42. }
  43. }

引用类:

  1. public class Address {
  2. String address;
  3. public String getAddress() {
  4. return address;
  5. }
  6. public void setAddress(String address) {
  7. this.address = address;
  8. }
  9. @Override
  10. public String toString() {
  11. return "Address{" +
  12. "address='" + address + '\'' +
  13. '}';
  14. }
  15. @Override
  16. protected Address clone() throws CloneNotSupportedException {
  17. return (Address)super.clone();
  18. }
  19. }

测试类:

  1. public class Test {
  2. public static void main(String[] args) throws CloneNotSupportedException {
  3. Address address = new Address();
  4. address.setAddress("汉中");
  5. Person p1 = new Person(100,"jim");
  6. p1.setAddress(address);
  7. Person p2 =p1.clone();
  8. p2.setName("tom");
  9. address.setAddress("西安");//
  10. System.out.println(p1);
  11. }
  12. }

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

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

引用类:

  1. public class Address implements Cloneable{
  2. String address;
  3. public String getAddress() {
  4. return address;
  5. }
  6. public void setAddress(String address) {
  7. this.address = address;
  8. }
  9. @Override
  10. public String toString() {
  11. return "Address{" +
  12. "address='" + address + '\'' +
  13. '}';
  14. }
  15. @Override
  16. protected Address clone() throws CloneNotSupportedException {
  17. return (Address)super.clone();
  18. }
  19. }

model类:

  1. public class Person implements Cloneable{
  2. int num;
  3. String name;
  4. Address address;
  5. public Person() {
  6. }
  7. public Person(int num, String name) {
  8. this.num = num;
  9. this.name = name;
  10. }
  11. public int getNum() {
  12. return num;
  13. }
  14. public void setNum(int num) {
  15. this.num = num;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public Address getAddress() {
  24. return address;
  25. }
  26. public void setAddress(Address address) {
  27. this.address = address;
  28. }
  29. @Override
  30. protected Person clone() throws CloneNotSupportedException {
  31. Person person = (Person)super.clone();
  32. person.address = (Address)address.clone(); //深度复制 联同person中关联的对象也一同克隆.
  33. return person;
  34. }
  35. @Override
  36. public String toString() {
  37. return "Person{" +
  38. "num=" + num +
  39. ", name='" + name + '\'' +
  40. ", address=" + address +
  41. '}';
  42. }
  43. }

测试:

  1. public class Test {
  2. public static void main(String[] args) throws CloneNotSupportedException {
  3. Address address = new Address();
  4. address.setAddress("汉中");
  5. Person p1 = new Person(100,"jim");
  6. p1.setAddress(address);
  7. Person p2 =p1.clone();
  8. p2.setName("tom");
  9. address.setAddress("西安");
  10. System.out.println(p1);
  11. System.out.println(p2);
  12. }
  13. }

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

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

引用类:

  1. public class Address implements Serializable {
  2. String address;
  3. public String getAddress() {
  4. return address;
  5. }
  6. public void setAddress(String address) {
  7. this.address = address;
  8. }
  9. @Override
  10. public String toString() {
  11. return "Address{" +
  12. "address='" + address + '\'' +
  13. '}';
  14. }
  15. }

model类:

  1. public class Person implements Serializable {
  2. int num;
  3. String name;
  4. Address address;
  5. public Person() {
  6. }
  7. public Person(int num, String name) {
  8. this.num = num;
  9. this.name = name;
  10. }
  11. public int getNum() {
  12. return num;
  13. }
  14. public void setNum(int num) {
  15. this.num = num;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public Address getAddress() {
  24. return address;
  25. }
  26. public void setAddress(Address address) {
  27. this.address = address;
  28. }
  29. /**
  30. * 自定义克隆方法
  31. * @return
  32. */
  33. public Person myclone() {
  34. Person person = null;
  35. try { // 将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
  36. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  37. ObjectOutputStream oos = new ObjectOutputStream(baos);
  38. oos.writeObject(this);
  39. // 将流序列化成对象
  40. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  41. ObjectInputStream ois = new ObjectInputStream(bais);
  42. person = (Person) ois.readObject();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. } catch (ClassNotFoundException e) {
  46. e.printStackTrace();
  47. }
  48. return person;
  49. }
  50. @Override
  51. public String toString() {
  52. return "Person{" +
  53. "num=" + num +
  54. ", name='" + name + '\'' +
  55. ", address=" + address +
  56. '}';
  57. }
  58. }

测试类:

  1. public class Test {
  2. public static void main(String[] args) throws CloneNotSupportedException {
  3. Address address = new Address();
  4. address.setAddress("汉中");
  5. Person p1 = new Person(100,"jim");
  6. p1.setAddress(address);
  7. Person p2 =p1.myclone();
  8. p2.setName("tom");
  9. address.setAddress("西安");
  10. System.out.println(p1);
  11. System.out.println(p2);
  12. }
  13. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/616672
推荐阅读
相关标签
  

闽ICP备14008679号