java 面向对象(四十一):反射(五)反射应用二:获取运行时类的完整结构
2024-08-24 12:49:28
我们可以通过反射,获取对应的运行时类中所有的属性、方法、构造器、父类、接口、父类的泛型、包、注解、异常等。。。。
典型代码:
@Test
public void test1(){ Class clazz = Person.class; //获取属性结构
//getFields():获取当前运行时类及其父类中声明为public访问权限的属性
Field[] fields = clazz.getFields();
for(Field f : fields){
System.out.println(f);
}
System.out.println(); //getDeclaredFields():获取当前运行时类中声明的所属性。(不包含父类中声明的属性
Field[] declaredFields = clazz.getDeclaredFields();
for(Field f : declaredFields){
System.out.println(f);
}
} @Test
public void test1(){ Class clazz = Person.class; //getMethods():获取当前运行时类及其所父类中声明为public权限的方法
Method[] methods = clazz.getMethods();
for(Method m : methods){
System.out.println(m);
}
System.out.println();
//getDeclaredMethods():获取当前运行时类中声明的所方法。(不包含父类中声明的方法
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
System.out.println(m);
}
} /*
获取构造器结构 */
@Test
public void test1(){ Class clazz = Person.class;
//getConstructors():获取当前运行时类中声明为public的构造器
Constructor[] constructors = clazz.getConstructors();
for(Constructor c : constructors){
System.out.println(c);
} System.out.println();
//getDeclaredConstructors():获取当前运行时类中声明的所的构造器
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for(Constructor c : declaredConstructors){
System.out.println(c);
} } /*
获取运行时类的父类 */
@Test
public void test2(){
Class clazz = Person.class; Class superclass = clazz.getSuperclass();
System.out.println(superclass);
} /*
获取运行时类的带泛型的父类 */
@Test
public void test3(){
Class clazz = Person.class; Type genericSuperclass = clazz.getGenericSuperclass();
System.out.println(genericSuperclass);
} /*
获取运行时类的带泛型的父类的泛型 代码:逻辑性代码 vs 功能性代码
*/
@Test
public void test4(){
Class clazz = Person.class; Type genericSuperclass = clazz.getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
//获取泛型类型
Type[] actualTypeArguments = paramType.getActualTypeArguments();
// System.out.println(actualTypeArguments[0].getTypeName());
System.out.println(((Class)actualTypeArguments[0]).getName());
} /*
获取运行时类实现的接口
*/
@Test
public void test5(){
Class clazz = Person.class; Class[] interfaces = clazz.getInterfaces();
for(Class c : interfaces){
System.out.println(c);
} System.out.println();
//获取运行时类的父类实现的接口
Class[] interfaces1 = clazz.getSuperclass().getInterfaces();
for(Class c : interfaces1){
System.out.println(c);
} }
/*
获取运行时类所在的包 */
@Test
public void test6(){
Class clazz = Person.class; Package pack = clazz.getPackage();
System.out.println(pack);
} /*
获取运行时类声明的注解 */
@Test
public void test7(){
Class clazz = Person.class; Annotation[] annotations = clazz.getAnnotations();
for(Annotation annos : annotations){
System.out.println(annos);
}
}
最新文章
- C# Async与Await的使用
- 进击的docker 二 : docker 快速入门
- [学习笔记]坚果云网盘,SVN异地代码管理
- 完美且精准的 IE10- 版本检测。
- openstack security group and rules python api use
- ABAP ALV单个单元格状态编辑-简单版本
- 循环编辑文件夹IBMEmptorisSSM-WSDL 下面的所有的wsdl文件到 d盘的wsdlSource下
- RAID阵列的初始化与管理
- Android常用布局
- 掌握jQuery插件开发,这篇文章就够了
- overlay
- Mysql数据库索引
- vs 2017
- VS2010 LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- IdentityServer开题篇
- Newtonsoft.Json日期转换
- Java初学者的30个常见问题
- CentOS双机中Docker下安装Mysql并配置互为主从模式
- Codeforces 652F Ants on a Circle
- Flash:移除匿名函数监听器EventListener
热门文章
- 百度编辑器UEditor不能插入视频的解决方法
- mysql忘记root密码后,重新设置、修改root密码
- 3 年经验的 Java 后端妹子,横扫阿里、滴滴、美团,整理出这份厚厚的 8000 字面经!
- OO第一单元——谜之随性总结
- 商城02——dubbo框架整合_商品列表查询实现_分页
- MySQL 视图 事务 索引 外连接
- MongoDB设计方法及技巧
- git提交时报错:Updates were rejected because the tip of your current branch is behind
- TestNG配合catubuter统计单元测试的代码覆盖率
- 06.DBUnit实际运用