![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
反射
反方向的钟c
一星程序员
展开
-
[反射]调用运行时类中的指定属性、方法、构造器
/* 调用运行时类中指定的结构:属性、方法、构造器 */ public class ReflectionTest1 { @Test public void testField() throws Exception { Class clazz = Person1.class; //创建运行时类的对象 Person1 p = (Person1) clazz.newInstance(); //获取指定的属性:要求运行时类中属性.原创 2022-05-15 19:49:49 · 73 阅读 · 0 评论 -
[反射]获取运行时类的接口、所在包、注解
/* 获取运行时类实现的接口 */ @Test public void test5(){ Class clazz = Person1.class; Class[] interfaces = clazz.getInterfaces(); for (Class c : interfaces){ System.out.println(c); } System.out.pr.原创 2022-05-15 17:36:06 · 93 阅读 · 0 评论 -
[反射]获取运行时类的父类及父类的泛型
/* 获取运行时类的父类 */ @Test public void test2(){ Class clazz = Person1.class; Class superclass = clazz.getSuperclass(); System.out.println(superclass); } /* 获取运行时类的带泛型的父类 */ @Test public void test3...原创 2022-05-15 17:20:45 · 121 阅读 · 0 评论 -
[反射]获取运行时类的方法结构及其内部结构
/* 获取运行时类的方法结构 */ public class MethodTest { @Test public void test1(){ Class clazz = Person1.class; //getMethods():获取当前运行时类及其所有父类中声明为public权限的方法 Method[] methods = clazz.getMethods(); for (Method m : methods){ .原创 2022-05-13 23:55:56 · 80 阅读 · 0 评论 -
[反射]获取运行时类的属性结构及其内部结构
//Creature类 public class Creature<T> implements Serializable { private char gender; public double weight; private void breath(){ System.out.println("生物呼吸"); } public void eat(){ System.out.println("生物吃东西"); .原创 2022-05-13 12:24:21 · 89 阅读 · 0 评论 -
[反射]使用ClassLoader加载配置文件
@Test public void test2() throws Exception { Properties pros = new Properties(); //此时的文件默认在当前的module下 //读取配置文件的方式一: // FileInputStream fis = new FileInputStream("jdbc.properties"); // pros.load(fis); ...原创 2022-05-12 22:52:54 · 216 阅读 · 0 评论 -
[反射]获取Class实例的4种方式
@Test public void test3() throws ClassNotFoundException { //方式一:调用运行时类的属性:.class Class clazz1 = Person.class; System.out.println(clazz1); //方式二:通过运行时类的对象,调用getClass() Person p1 = new Person(); Class cl..原创 2022-05-12 22:46:42 · 248 阅读 · 0 评论 -
[反射]使用反射调用类的私有结构
/* Person类 */ public class Person { private String name; public int age; public Person() { } private Person(String name) { this.name = name; } public Person(String name, int age) { this.name = name; .原创 2022-05-10 23:04:57 · 147 阅读 · 0 评论