• <big id="vhnbr"></big>

      注解

      定義: 注解是一種注釋機制,它可以注釋包、類、方法、變量、參數,在編譯器生成類文件時,標注可以被嵌入到字節碼中。

      注解的分類:

      內置注解

      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出來的對象屬性都是初始化的值,不能保存當前對象“狀態”,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

      相關文章:

      国产免费三A级精品大片|久久久久久女乱国产|操逼无码播放六区|黄色毛片在线观看视频

    1. <big id="vhnbr"></big>