<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Class對象的獲取

      三種方式獲取Class對象

      1. 調用對象的getClass方法
      2. 調用類名.class
      3. Class.forName("類的全名"), 這種方式是去JVM中查找,可能會拋出ClassNotFoundException
      public class SubPerson extends Person {
      
          public Long subId;
          private String subName;
          private SubPerson() {
          }
          public SubPerson(Long subId, String subName) {
              this.subId = subId;
              this.subName = subName;
          }
      
      }
      
      public class Person {
      
          public Long id;
          private String name;
          public Person() {
          }
      
          class Card{
              private String id;
              public Card() {
              }
              public String getId() {
                  return id;
              }
              public void setId(String id) {
                  this.id = id;
              }
          }
      }
      
      
      //1
      Class personClass1 = new Person().getClass();
       System.out.println(personClass1);
       
       //2
       Class personClass2 = Person.class;
       System.out.println(personClass2);
       
       //3
       try {
           Class personClass3 = Class.forName("com.banary.reflect.Person");
           System.out.println(personClass3);
       }catch (ClassNotFoundException e){
           e.printStackTrace();
       }
      

      獲取類名

      public static void ClassMethodTest(){
          //獲取全類名
          System.out.println(Person.class.getName());
          System.out.println(int.class.getName());
          System.out.println(int[][][].class.getName());
          System.out.println(Person.Card.class.getName());
          System.out.println(new Runnable() {
              @Override
              public void run() {
              }
          }.getClass().getName());
      
          Runnable runnable = ()-> System.out.println("test");
          System.out.println(runnable.getClass().getName());
      
          System.out.println("------------分割線------------");
      
          //獲取類名
          System.out.println(Person.class.getSimpleName());
          System.out.println(int.class.getSimpleName());
          System.out.println(int[][][].class.getSimpleName());
          System.out.println(Person.Card.class.getSimpleName());
          System.out.println(new Runnable() {
              @Override
              public void run() {
              }
          }.getClass().getSimpleName());
          System.out.println(runnable.getClass().getSimpleName());
      
          System.out.println("------------分割線------------");
      
          /**
           * 獲取官方類名
           * 1. getCanonicalName() 返回的也是全限定類名,但是對于內部類,不用 $ 開頭,而用 .。
           * 2. getCanonicalName() 對于數組類型的 Class,同 simplename 一樣直接在后面添加 [] 。
           * 3. getCanonicalName() 不同于 simplename 的地方是,不存在 canonicalName 的時候返回 null 而不是空字符串。
           * 4. 局部類和匿名內部類不存在 canonicalName。
           */
          System.out.println(Person.class.getCanonicalName());
          System.out.println(int.class.getCanonicalName());
          System.out.println(int[][][].class.getCanonicalName());
          System.out.println(Person.Card.class.getCanonicalName());
          System.out.println(new Runnable() {
              @Override
              public void run() {
              }
          }.getClass().getCanonicalName());
          System.out.println(runnable.getClass().getCanonicalName());
      }
      

      獲取修飾符

      1. java的修飾符
      • 用來限制作用域,如 public、protected、priviate
      • 用來提示子類復寫,abstract
      • 用來標記為靜態類 static
      • 注解
      • 定義常量,final
      1. 獲取類的修飾符
      // 十進制
      System.out.println(Modifier.class.getModifiers());
      // 十六進制
      System.out.println(Integer.toHexString(Modifier.class.getModifiers()));
      // 對應的真實修飾符
      System.out.println(java.lang.reflect.Modifier.toString(Modifier.class.getModifiers()));
      

      java中修飾符的表示方式

      通過上述的方法,可見java中是采用二進制位來表示修飾符,下列為java中修飾符實現

      public class Modifier {
      
          // 2^0
          public static final int PUBLIC           = 0x00000001;
          // 2^1
          public static final int PRIVATE          = 0x00000002;
          // 2^2
          public static final int PROTECTED        = 0x00000004;
          public static final int STATIC           = 0x00000008;
          public static final int FINAL            = 0x00000010;
          public static final int SYNCHRONIZED     = 0x00000020;
          public static final int VOLATILE         = 0x00000040;
          public static final int TRANSIENT        = 0x00000080;
          public static final int NATIVE           = 0x00000100;
          public static final int INTERFACE        = 0x00000200;
          public static final int ABSTRACT         = 0x00000400;
          public static final int STRICT           = 0x00000800;
          
          public static String toString(int mod) {
              StringBuilder sb = new StringBuilder();
              int len;
      
              if ((mod & PUBLIC) != 0)        sb.append("public ");
              if ((mod & PROTECTED) != 0)     sb.append("protected ");
              if ((mod & PRIVATE) != 0)       sb.append("private ");
      
              /* Canonical order */
              if ((mod & ABSTRACT) != 0)      sb.append("abstract ");
              if ((mod & STATIC) != 0)        sb.append("static ");
              if ((mod & FINAL) != 0)         sb.append("final ");
              if ((mod & TRANSIENT) != 0)     sb.append("transient ");
              if ((mod & VOLATILE) != 0)      sb.append("volatile ");
              if ((mod & SYNCHRONIZED) != 0)  sb.append("synchronized ");
              if ((mod & NATIVE) != 0)        sb.append("native ");
              if ((mod & STRICT) != 0)        sb.append("strictfp ");
              if ((mod & INTERFACE) != 0)     sb.append("interface ");
      
              if ((len = sb.length()) > 0)    /* trim trailing space */
                  return sb.toString().substring(0, len-1);
              return "";
          }
      
      }
      

      獲取類的屬性

      類的屬性包括Field、Method、Constructor

      1. Field
      • 獲取Field
      Class personClass = SubPerson.class;
      //獲取自身的所有的 public 屬性,包括從父類繼承下來的, 可能會拋出運行時異常SecurityException
      Field[] fields = personClass.getFields();
      for (Field field : fields) {
          System.out.println(field.getName());
      }
      //根據field名獲取, 可能會拋出NoSuchFieldException(檢查型), SecurityException
      try {
          Field field1 = personClass.getField("subId");
          System.out.println(field1.getName());
      } catch (NoSuchFieldException e){
          e.printStackTrace();
      }
      
      System.out.println("------------分割線------------");
      
      //獲取所有的屬性,但不包括從父類繼承下來的屬性
      Field[] declaredFields = personClass.getDeclaredFields();
      for (Field declaredField : declaredFields) {
          System.out.println(declaredField.getName());
      }
      
      System.out.println("------------分割線------------");
      
      try{
          Field field2 = personClass.getDeclaredField("subName");
          System.out.println(field2.getName());
      } catch (NoSuchFieldException e){
          e.printStackTrace();
      }
      
      • 獲取Field類型
      # 能夠獲取到泛型類型
      public Type getGenericType() {}
      
      public Class<?> getType() {}
      
      • 獲取修飾符
      # 同class和Method的修飾符一樣
      public int getModifiers() {}
      
      • 獲取值和設置值
      #設置值
      public Object get(Object obj);
      public int getInt(Object obj);
      public long getLong(Object obj) throws IllegalArgumentException, IllegalAccessException;
      public float getFloat(Object obj) throws IllegalArgumentException, IllegalAccessException;
      public short getShort(Object obj) throws IllegalArgumentException, IllegalAccessException;
      public double getDouble(Object obj) throws IllegalArgumentException, IllegalAccessException;
      public char getChar(Object obj) throws IllegalArgumentException, IllegalAccessException;
      public byte getByte(Object obj) throws IllegalArgumentException, IllegalAccessException;
      public boolean getBoolean(Object obj) throws IllegalArgumentException, IllegalAccessException
      public void set(Object obj, Object value);
      # 獲取值
      public void getInt(Object obj,int value);
      public void getLong(Object obj,long value) throws IllegalArgumentException, IllegalAccessException;
      public void getFloat(Object obj,float value) throws IllegalArgumentException, IllegalAccessException;
      public void getShort(Object obj,short value) throws IllegalArgumentException, IllegalAccessException;
      public void getDouble(Object obj,double value) throws IllegalArgumentException, IllegalAccessException;
      public void getChar(Object obj,char value) throws IllegalArgumentException, IllegalAccessException;
      public void getByte(Object obj,byte b) throws IllegalArgumentException, IllegalAccessException;
      public void getBoolean(Object obj,boolean b) throws IllegalArgumentException, IllegalAccessException
      

      Class 本身不對成員進行儲存,它只提供檢索,所以需要用 Field、Method、Constructor 對象來承載這些成員,所以,針對成員的操作時,一般需要為成員指定類的實例引用。

      1. Method
        同獲取Field類似
      • 方法名
      getName()
      
      • 方法參數
      #獲取所有的參數
      public Parameter[] getParameters() {}
      // 獲取所有的參數類型
      public Class<?>[] getParameterTypes() {}
      // 獲取所有的參數類型,包括泛型
      public Type[] getGenericParameterTypes() {}
      
      • 方法返回值
      // 獲取返回值類型
      public Class<?> getReturnType() {}
      // 獲取返回值類型包括泛型
      public Type getGenericReturnType() {}
      
      • 方法的修飾符
      public int getModifiers() {}
      
      • 方法可能會拋出的異常
      public Class<?>[] getExceptionTypes() {}
      # 包括泛型
      public Type[] getGenericExceptionTypes() {}
      
      • 執行方法
      #invoke() 方法中第一個參數 Object 實質上是 Method 所依附的 Class 對應的類的實例,如果這個方法是一個靜態方法,那么 ojb 為 null,后面的可變參數 Object 對應的自然就是參數
      #invoke() 返回的對象是 Object,所以實際上執行的時候要進行強制轉換。
      #在對 Method 調用 invoke() 的時候,如果方法本身會拋出異常,那么這個異常就會經過包裝,由 Method 統一拋出 InvocationTargetException。而通過 InvocationTargetException.getCause() 可以獲取真正的異常。
      public Object invoke(Object obj, Object... args) {}
      
      1. Constructor
      • 獲取構造器方法
        同獲取Field類似,但是由于構造器不能繼承,所以getConstructor()方法獲取不到父類的構造器方法
      public static void getConstructor(){
              Class subPersonClass = SubPerson.class;
      
              //獲取所有的public構造器,由于父類的構造器不能繼承,所以獲取不到
              Constructor[] constructors = subPersonClass.getConstructors();
              for (Constructor c : constructors) {
                  System.out.println(c.toString());
              }
      
              System.out.println("------------分割線------------");
      
              //獲取所有的構造器,包括私有和公開的
              Constructor[] declaredConstructors = subPersonClass.getDeclaredConstructors();
              for (Constructor declaredConstructor :declaredConstructors) {
                  System.out.println(declaredConstructor.toString());
              }
          }
      
      • 創建對象
        在 Java 反射機制中有兩種方法可以用來創建類的對象實例:Class.newInstance() 和 Constructor.newInstance(),原因:
      # Class.newInstance() 只能調用無參的構造方法,而 Constructor.newInstance() 則可以調用任意的構造方法。
      # Class.newInstance() 通過構造方法直接拋出異常,而 Constructor.newInstance() 會把拋出來的異常包裝到 InvocationTargetException 里面去,這個和 Method 行為一致。
      # Class.newInstance() 要求構造方法能夠被訪問,而 Constructor.newInstance() 卻能夠訪問 private 修飾的構造器。
      

      反射中的數組

      • 數組本質上是一個 Class,而在 Class 中存在一個方法isArray()用來識別它是否為一個數組。
      getName();
      #獲取數組的里面的元素的類型,比如 int[] 數組的 componentType 自然就是 int
      getComponentType();
      
      • 反射中動態創建數組
        反射創建數組是通過 Array.newInstance() 這個方法
      public static Object newInstance(Class<?> componentType, int... dimensions)
              throws IllegalArgumentException, NegativeArraySizeException {}
      

      e.g.

      # 創建一個 int[2][3] 的數組
      Array.newInstance(int.class,2,3);
      
      • Array 的讀取與賦值
      public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException;
      
      public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException;
      
      public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
      
      public static void setBoolean(Object array, int index, boolean z) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
      
      public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
      
      public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
      

      反射中的枚舉

      • 同數組一樣,枚舉本質上也是一個 Class 而已,但反射中還是把它單獨提出來了
      // 用來判定 Class 對象是不是枚舉類型
      Class.isEnum()
      
      // 獲取所有的枚舉常量
      Class.getEnumConstants()
      
      // 判斷一個 Field 是不是枚舉常量
      Field.isEnumConstant()
      
      • 枚舉的獲取與設定
        因為等同于 Class,所以枚舉的獲取與設定就可以通過 Field 中的 get() 和 set() 方法。需要注意的是,如果要獲取枚舉里面的 Field、Method、Constructor 可以調用 Class 的通用 API,值得注意的是,枚舉類的Constructor可以獲取,但是不能實例化
      public static void reflectEnum() throws Exception{
              Class colorClass = Color.class;
              if(colorClass.isEnum()){
                  System.out.println(String.format("class Color %s is Enum", colorClass.getName()));
                  Field[] fields = colorClass.getDeclaredFields();
                  for (Field field:fields) {
                      if(field.isEnumConstant()){
                          System.out.println(String.format("%s is EnumConstant", field.getName()));
                      }else{
                          System.out.println(String.format("%s is not EnumConstant", field.getName()));
                      }
                  }
              }
      
              System.out.println("_____________________");
      
              Constructor[] constructors = colorClass.getDeclaredConstructors();
              for(Constructor constructor : constructors){
                  System.out.println(constructor.getName());
                  constructor.setAccessible(true);
                  Color color = (Color) constructor.newInstance("123123");
                  System.out.println(color.getKey());
              }
              Color color = (Color) colorClass.newInstance();
              System.out.println(color.getKey());
          }
      
      posted on 2017-12-07 16:54  行走在云端的愚公  閱讀(366)  評論(0)    收藏  舉報

      主站蜘蛛池模板: 国内精品久久人妻无码不卡| 任我爽精品视频在线播放| 国产人妻人伦精品婷婷| 精品国产午夜福利在线观看| 中文字幕在线日韩| 国产精品免费看久久久无码| 亚洲精品乱码久久久久久中文字幕| 狠狠色丁香婷婷久久综合五月| 国产精品一区二区不卡91| 欧美一本大道香蕉综合视频| 久久精品国产99亚洲精品| 国产精品中文第一字幕| 国产999精品2卡3卡4卡| av鲁丝一区鲁丝二区鲁丝三区 | 国产成人午夜在线视频极速观看| 视频一区二区三区高清在线| 最新午夜男女福利片视频| 久久人妻国产精品| 欧美一区二区三区在线观看| 国产精品一区二区三区日韩| 国产欧美日韩在线在线播放| 99久久精品国产一区色| 2019国产精品青青草原| 欧美怡春院一区二区三区| 国产在线精品欧美日韩电影| 午夜一区二区三区视频| 成在人线av无码免费看网站直播 | 成年在线观看免费人视频 | 成人免费无码视频在线网站| 三上悠亚精品一区二区久久| 精品无码久久久久久久动漫| 国产精品第一页中文字幕| 人妻中文字幕精品一页| 国产综合有码无码中文字幕| 国内精品伊人久久久影视| 国产精品无码无卡在线播放| 色欲av久久一区二区三区久| 海门市| japanese无码中文字幕| 黑人猛精品一区二区三区| 无码人妻斩一区二区三区|