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类相关推荐
- 利用java反射调用类的的私有方法
http://blog.csdn.net/sunyujia/article/details/2501709 今天和一位朋友谈到父类私有方法的调用问题,本来以为利用反射很轻松就可以实现,因为在反射看来根 ...
- 利用java反射调用类的的私有方法--转
原文:http://blog.csdn.net/woshinia/article/details/11766567 1,今天和一位朋友谈到父类私有方法的调用问题,本来以为利用反射很轻松就可以实现,因为 ...
- Java反射 Constructor类
Java反射 Constructor类 Java通过反射可以获取构造方法,和使用构造方法创造对象. 在网上找了一个比较好理解的例子来弄清楚Constructor类. public Constructo ...
- java 反射 本类_Java 反射 Class类
Java 反射 Class类 @author ixenos 摘要:Class类在反射中的地位.构造Class对象的三种方式.Class对象构造对应类型对象的三种方式 Class类在反射中的地位 位于j ...
- java反射 数组类,乐字节Java反射之三:方法、数组、类加载器和类的生命周期
继续讲述Java反射之三:方法.数组.类加载器 一.方法 获取所有方法(包括父类或接口),使用Method即可. public static void test() throwsException { ...
- Java 反射取类中类_Java反射机制(二):通过反射取得类的结构
在反射运用过程中,如果你想得到一个类的完整结构,那么就要使用到java.lang.reflect包中的几个类: · Constructor 表示类中的构造方法 · Field 表示类中的属性 · ...
- Cathy学习Java——反射和类的加载
工厂设计模式 工厂方法模式 概述 工厂:就是生产特点产品的 实现方式 1>创建一个抽象工厂类,声明抽象方法 2>写一个具体抽象工厂类的子类,由子类负责对象的创建 优点:后期容易维护,增强了 ...
- Java反射 Class类常用方法详解
获取一个类对应的Class类的方法 1.使用Object.getClass ()方法----引用类型的对象的获取方式 如果我们已经拿到了一个对象,可以使用这个对象的 getClass 方法获得一个 C ...
- java 反射操作工具类
导包 <!-- 谷歌公共依赖核心库 --><dependency><groupId>com.google.guava</groupId><arti ...
最新文章
- rest-framework之解析器
- 如何真正理解用Nginx代理来解决同源策略
- windows 常用工具
- Filter获取Spring Bean对象
- Qt中 QString 和int,double等的转换
- ictclas4j 分词工具包 安装流程
- c++中的运算符异或^,与,或|
- jquery获取input值
- FindChildControl与FindComponent(动态创建的控件要通过Owner.FindComponent去找该控件)
- 实用机器人设计(二)-传感器
- Linux性能优化实战:CPU的上下文切换是什么意思(03)
- java自动填写网页表格,excel表格调用网页数据库-如何用Excel自动填写网页数据
- 数字化转型对企业的意义
- CSS如何进行图片定位
- 2021-08-04 jQuery基础整理 17-30 代码复制即可运行
- C++中不能重载的运算符
- 计算机网络基础之计算机网络
- 【Linux】文件类型
- ipad在线看html5,iPad 2: HTML5开发者必备首选
- 以给定值x为基准将单链表分割为两部分,所有小于x的结点都排在大于或等于x的结点之前。
热门文章
- zoj 1091 Knight Moves
- 关于BigInteger的加减乘除使用
- 河北微型计算机原理专接本,河北省专接本(微机原理与接口技术知识点总结)
- gossip 区块链_区块链中的P2P
- 七度空间338多少钱一包_2020黄果树香烟一包多少钱 黄果树香烟价格表图排行榜...
- Win10系统电脑不会一键还原系统怎么解决
- Win11怎么设置提高电脑游戏性能
- 积米浏览器如何清除浏览数据
- python创建变量revenue、并赋值为98765_第七章:Python之数据库编程
- Linux nohup实现后台运行程序及查看(nohup与)