日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java编程:Java的反射机制中的 getComponentType() 方法

發(fā)布時間:2023/12/3 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java编程:Java的反射机制中的 getComponentType() 方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載自??Java編程:Java的反射機制中的 getComponentType() 方法

?

Java 中所有的類都繼承自 Object,數(shù)組本身也是一個 Class,如果我們能夠得到數(shù)據(jù)的 Class 對象,那么我們可以通過反射生成數(shù)組對象。

在Java的反射機制中,通過 數(shù)組的 class 對象的getComponentType()方法可以取得一個數(shù)組的Class對象, 通過Array.newInstance()可以反射生成數(shù)組對象,看示例代碼:

package com.ips.reflect;import java.lang.reflect.Array;public class GetComponentType {public static void main(String [] args){Class<char[]> aa = (Class<char[]>) char.class.getComponentType();System.out.println("the componentType of the char is :" + char.class.getComponentType());System.out.println("the componentType of the char[] is :" + char[].class.getComponentType());System.out.println("the componentType of the String is :" + String.class.getComponentType());System.out.println("the componentType of the String[] is :" + String[].class.getComponentType());System.out.println("the componentType of the int is :" + int.class.getComponentType());System.out.println("the componentType of the int[] is :" + int[].class.getComponentType());System.out.println("the componentType of the Integer is :" + Integer.class.getComponentType());System.out.println("the componentType of the Integer[] is :" + Integer[].class.getComponentType());try {char c = (char)Array.newInstance(char.class.getComponentType(), 10);} catch (Exception e) {e.printStackTrace();}char[] charArray = (char [])Array.newInstance(char[].class.getComponentType(), 100);System.out.println("the length of the charArray is :" + charArray.length);try {String c = (String)Array.newInstance(String.class.getComponentType(), 10);} catch (Exception e) {e.printStackTrace();}String[] strArray = (String [])Array.newInstance(String[].class.getComponentType(), 10);System.out.println("the length of the strArray is :" + strArray.length);} }

執(zhí)行結(jié)果:

the componentType of the char is :null?
the componentType of the char[] is :char?
the componentType of the String is :null?
the componentType of the String[] is :class java.lang.String?
the componentType of the int is :null?
the componentType of the int[] is :int?
the componentType of the Integer is :null?
the componentType of the Integer[] is :class java.lang.Integer?
java.lang.NullPointerException?
at java.lang.reflect.Array.newArray(Native Method)?
at java.lang.reflect.Array.newInstance(Array.java:70)?
at com.ips.reflect.GetComponentType.main(GetComponentType.java:23)?
the length of the charArray is :100?
java.lang.NullPointerException?
at java.lang.reflect.Array.newArray(Native Method)?
at java.lang.reflect.Array.newInstance(Array.java:70)?
at com.ips.reflect.GetComponentType.main(GetComponentType.java:31)?
the length of the strArray is :10

通過以上代碼我們可以發(fā)現(xiàn):

  • 非數(shù)組類型無法通過getComponentType()獲取到相應(yīng)的 Class 對象;
  • 非數(shù)組類型無法通過(String [])Array.newInstance(String[].class.getComponentType(), 10)方法反射生成數(shù)組對象,因為獲取不到 Class 對象;
  • 無論基本數(shù)據(jù)類型(byte short int long float double char boolean)的數(shù)組還是引用數(shù)據(jù)類型(Integer String 等)的數(shù)組,都可以通過getComponentType()獲取到相應(yīng)的 Class 對象,都可以通過(String [])Array.newInstance(String[].class.getComponentType(), 10)方法反射生成數(shù)組對象。
  • ?

    總結(jié)

    以上是生活随笔為你收集整理的Java编程:Java的反射机制中的 getComponentType() 方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。