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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java反射 Class类

發布時間:2025/3/12 java 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java反射 Class类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Class類的實例表示正在運行的Java應用程序中的類和接口。它是Java反射的基礎,對任何一個類,首先產生一個Class對象,然后才通過class類獲得其他的信息。

獲取class類對象方式:

  • 通過Object類提供的getClass()方法獲得Class類對象。

    Object obj = new Object();//創建Object類對象 Class c1 = obj.getClass();//調用Object類的getClass()方法獲得Class類對象
  • 通過Class類的靜態方法forName()獲取字符串參數指定的Class類對象。

    Class c2 = Class.forName("java.lang.Integer");//參數必須是類或接口的全名,包含類名和包名,并注意捕獲ClassNotFoundException異常
  • 通過類名.class獲取該類的Class對象

    Class c3 = Integer.class;

Class類常用方法:

  • getPackage(),返回此類的包,返回類型:package。
  • getModifiers(),返回此類的Java語言修飾符,返回類型:int。 修飾符由Java虛擬機的常數為public , protected , private , final , static , abstract和interface ; 應使用Modifier類的方法進行解碼。
  • getName(),返回此類的全名,返回類型:String。
  • getDeclaredFields(),返回此類的所有字段,返回類型:Field[]
  • getDeclaredConstructors(),返回此類的所有構造方法,返回類型getDeclaredConstructors[]
  • import java.lang.reflect.*;public class ClassTest {public static void main(String args[]) {try {//返回指定字符串的類或接口的Class對象Class c = Class.forName("java.util.Date");Package p = c.getPackage();//返回此類的包String pname = p.getName();System.out.println("Data 類包信息:" + p);//讀取類包信息。輸出:Data 類包信息:package java.util, Java Platform API Specification, version 1.8System.out.println("Data 類包名" + pname);//讀取此類的包名。輸出:Data 類包名java.utilint m = c.getModifiers();//獲取類的修飾符String str = Modifier.toString(m);System.out.println("Data 類修飾符:" + str);//輸出:Data 類修飾符:publicSystem.out.println("Data 類名" + c.getName());//獲取類名。輸出:Data 類名java.util.Date//獲取Data類的字段。Field[] f = c.getDeclaredFields();System.out.println("---循環輸出Data類的字段名---");for(Field field : f) {System.out.print(field.getName() + " ");}//輸出:gcal jcal fastTime cdate defaultCenturyStart serialVersionUID wtb ttb System.out.println();//獲取類的構造方法Constructor[] con = c.getDeclaredConstructors();System.out.println("---循環輸出Data類的構造方法信息---");for(Constructor cc : con) {System.out.println(cc.getName() + "的修飾符:" + Modifier.toString(cc.getModifiers()));Parameter[] ps = cc.getParameters();System.out.println(cc.getName() + "的參數: ");for(Parameter pp : ps) {System.out.print(pp.getName() + " ");}System.out.println();}}catch(ClassNotFoundException e) {e.printStackTrace();}} } /** Data 類包信息:package java.util, Java Platform API Specification, version 1.8 Data 類包名java.util Data 類修飾符:public Data 類名java.util.Date ---循環輸出Data類的字段名--- gcal jcal fastTime cdate defaultCenturyStart serialVersionUID wtb ttb ---循環輸出Data類的構造方法信息--- java.util.Date的修飾符:public java.util.Date的參數: arg0 arg1 arg2 arg3 arg4 arg5 java.util.Date的修飾符:public java.util.Date的參數: arg0 java.util.Date的修飾符:public java.util.Date的參數: java.util.Date的修飾符:public java.util.Date的參數: arg0 java.util.Date的修飾符:public java.util.Date的參數: arg0 arg1 arg2 java.util.Date的修飾符:public java.util.Date的參數: arg0 arg1 arg2 arg3 arg4 */

    然后我們對比Data的源碼中的字段和構造函數,可以發現通過反射的getDeclaredConstructors()可以判斷Data源碼六個構造函數,再查看源碼,果然有六個構造函數。

    public class Dateimplements java.io.Serializable, Cloneable, Comparable<Date> {private static final BaseCalendar gcal =CalendarSystem.getGregorianCalendar();private static BaseCalendar jcal;private transient long fastTime;/** If cdate is null, then fastTime indicates the time in millis.* If cdate.isNormalized() is true, then fastTime and cdate are in* synch. Otherwise, fastTime is ignored, and cdate indicates the* time.*/private transient BaseCalendar.Date cdate;// Initialized just before the value is used. See parse().private static int defaultCenturyStart;/* use serialVersionUID from modified java.util.Date for* interoperability with JDK1.1. The Date was modified to write* and read only the UTC time.*/private static final long serialVersionUID = 7523967970034938905L;/*** Allocates a <code>Date</code> object and initializes it so that* it represents the time at which it was allocated, measured to the* nearest millisecond.** @see java.lang.System#currentTimeMillis()*/public Date() {this(System.currentTimeMillis());}/*** Allocates a <code>Date</code> object and initializes it to* represent the specified number of milliseconds since the* standard base time known as "the epoch", namely January 1,* 1970, 00:00:00 GMT.** @param date the milliseconds since January 1, 1970, 00:00:00 GMT.* @see java.lang.System#currentTimeMillis()*/public Date(long date) {fastTime = date;}/*** Allocates a <code>Date</code> object and initializes it so that* it represents midnight, local time, at the beginning of the day* specified by the <code>year</code>, <code>month</code>, and* <code>date</code> arguments.** @param year the year minus 1900.* @param month the month between 0-11.* @param date the day of the month between 1-31.* @see java.util.Calendar* @deprecated As of JDK version 1.1,* replaced by <code>Calendar.set(year + 1900, month, date)</code>* or <code>GregorianCalendar(year + 1900, month, date)</code>.*/@Deprecatedpublic Date(int year, int month, int date) {this(year, month, date, 0, 0, 0);}/*** Allocates a <code>Date</code> object and initializes it so that* it represents the instant at the start of the minute specified by* the <code>year</code>, <code>month</code>, <code>date</code>,* <code>hrs</code>, and <code>min</code> arguments, in the local* time zone.** @param year the year minus 1900.* @param month the month between 0-11.* @param date the day of the month between 1-31.* @param hrs the hours between 0-23.* @param min the minutes between 0-59.* @see java.util.Calendar* @deprecated As of JDK version 1.1,* replaced by <code>Calendar.set(year + 1900, month, date,* hrs, min)</code> or <code>GregorianCalendar(year + 1900,* month, date, hrs, min)</code>.*/@Deprecatedpublic Date(int year, int month, int date, int hrs, int min) {this(year, month, date, hrs, min, 0);}/*** Allocates a <code>Date</code> object and initializes it so that* it represents the instant at the start of the second specified* by the <code>year</code>, <code>month</code>, <code>date</code>,* <code>hrs</code>, <code>min</code>, and <code>sec</code> arguments,* in the local time zone.** @param year the year minus 1900.* @param month the month between 0-11.* @param date the day of the month between 1-31.* @param hrs the hours between 0-23.* @param min the minutes between 0-59.* @param sec the seconds between 0-59.* @see java.util.Calendar* @deprecated As of JDK version 1.1,* replaced by <code>Calendar.set(year + 1900, month, date,* hrs, min, sec)</code> or <code>GregorianCalendar(year + 1900,* month, date, hrs, min, sec)</code>.*/@Deprecatedpublic Date(int year, int month, int date, int hrs, int min, int sec) {int y = year + 1900;// month is 0-based. So we have to normalize month to support Long.MAX_VALUE.if (month >= 12) {y += month / 12;month %= 12;} else if (month < 0) {y += CalendarUtils.floorDivide(month, 12);month = CalendarUtils.mod(month, 12);}BaseCalendar cal = getCalendarSystem(y);cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef());cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0);getTimeImpl();cdate = null;}/*** Allocates a <code>Date</code> object and initializes it so that* it represents the date and time indicated by the string* <code>s</code>, which is interpreted as if by the* {@link Date#parse} method.** @param s a string representation of the date.* @see java.text.DateFormat* @see java.util.Date#parse(java.lang.String)* @deprecated As of JDK version 1.1,* replaced by <code>DateFormat.parse(String s)</code>.*/@Deprecatedpublic Date(String s) {this(parse(s));}//以下省略//。。。。。。。。。。。。。

    總結

    以上是生活随笔為你收集整理的Java反射 Class类的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。