网站标题优化技巧,手机兼职免费加入不需要任何费用,长春市建设厅网站,安徽城乡建设厅官网站Class类的实例表示正在运行的Java应用程序中的类和接口。它是Java反射的基础#xff0c;对任何一个类#xff0c;首先产生一个Class对象#xff0c;然后才通过class类获得其他的信息。 
获取class类对象方式#xff1a; 通过Object类提供的getClass()方法获得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, ComparableDate
{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 codeDate/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 codeDate/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 codeDate/code object and initializes it so that* it represents midnight, local time, at the beginning of the day* specified by the codeyear/code, codemonth/code, and* codedate/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 codeCalendar.set(year  1900, month, date)/code* or codeGregorianCalendar(year  1900, month, date)/code.*/Deprecatedpublic Date(int year, int month, int date) {this(year, month, date, 0, 0, 0);}/*** Allocates a codeDate/code object and initializes it so that* it represents the instant at the start of the minute specified by* the codeyear/code, codemonth/code, codedate/code,* codehrs/code, and codemin/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 codeCalendar.set(year  1900, month, date,* hrs, min)/code or codeGregorianCalendar(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 codeDate/code object and initializes it so that* it represents the instant at the start of the second specified* by the codeyear/code, codemonth/code, codedate/code,* codehrs/code, codemin/code, and codesec/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 codeCalendar.set(year  1900, month, date,* hrs, min, sec)/code or codeGregorianCalendar(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 codeDate/code object and initializes it so that* it represents the date and time indicated by the string* codes/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 codeDateFormat.parse(String s)/code.*/Deprecatedpublic Date(String s) {this(parse(s));}//以下省略//。。。。。。。。。。。。。