Java反射 Class类
生活随笔
收集整理的這篇文章主要介紹了
Java反射 Class类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Class類的實(shí)例表示正在運(yùn)行的Java應(yīng)用程序中的類和接口。它是Java反射的基礎(chǔ),對(duì)任何一個(gè)類,首先產(chǎn)生一個(gè)Class對(duì)象,然后才通過class類獲得其他的信息。
獲取class類對(duì)象方式:
-
通過Object類提供的getClass()方法獲得Class類對(duì)象。
Object obj = new Object();//創(chuàng)建Object類對(duì)象 Class c1 = obj.getClass();//調(diào)用Object類的getClass()方法獲得Class類對(duì)象 -
通過Class類的靜態(tài)方法forName()獲取字符串參數(shù)指定的Class類對(duì)象。
Class c2 = Class.forName("java.lang.Integer");//參數(shù)必須是類或接口的全名,包含類名和包名,并注意捕獲ClassNotFoundException異常 -
通過類名.class獲取該類的Class對(duì)象
Class c3 = Integer.class;
Class類常用方法:
然后我們對(duì)比Data的源碼中的字段和構(gòu)造函數(shù),可以發(fā)現(xiàn)通過反射的getDeclaredConstructors()可以判斷Data源碼六個(gè)構(gòu)造函數(shù),再查看源碼,果然有六個(gè)構(gòu)造函數(shù)。
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));}//以下省略//。。。。。。。。。。。。。總結(jié)
以上是生活随笔為你收集整理的Java反射 Class类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: api idea 开发rest_部分介绍
- 下一篇: aix查看oracle数据库端口号,通过