Class对象:
Class对象记录了所有与类相关的信息,当类加载器从文件系统中加载.class文件到JVM中的同时会为每一个类创建一个Class对象。通过Class对象可以获取到类的属性、方法、构造器等全部与类相关的信息。
现在假设在com.aop包下有一个抽象类和一个类,定义如下:
package com.aop; abstract public class Creature<T> { }
package com.aop; public class Person extends Creature<String>{ int id;
public String name;
private int age; public Person() { }
public Person(int age, String name){
this.age = age;
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public int getAge() {
return age;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} static public void countAdd(int a, Integer b){
System.out.println(a + " + " + b + " = " + (a+b));
} public void show() {
System.out.println("我是一个人!");
} public void display(String nation) {
System.out.println("我的国际是:" + nation);
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
} }
很简洁,定义一个带泛型的抽象类,Person继承自该抽象类。
与反射相关的包:
import java.lang.reflect.Constructor; //与构造器相关
import java.lang.reflect.Field; //与域相关
import java.lang.reflect.Method; //与方法相关
//还有其它的...
1、Class对象的4中获取方式:
package com.aop; import org.junit.Test; public class TestReflection { @Test
public void testReflection() throws Exception{ //1.调用运行时类本身的.class属性
Class<Person> clazz1 = Person.class; //2.通过运行时类的对象获取
Person person = new Person();
Class<? extends Person> clazz2 = person.getClass(); //3.通过Class的静态方法获取.通过此方式,体会一下,反射的动态性。
String className = "com.aop.Person"; Class<?> clazz3 = Class.forName(className); //4.(了解)通过类的加载器
ClassLoader loader = this.getClass().getClassLoader();
Class<?> clazz4 = loader.loadClass(className); System.out.println(clazz1.getName());
System.out.println(clazz2.getName());
System.out.println(clazz3.getName());
System.out.println(clazz4.getName()); System.out.println(clazz1 == clazz2);
System.out.println(clazz1 == clazz3);
System.out.println(clazz1 == clazz4);
}
}
2、通过反射读取对象的属性值,并修改它们(能够读取与修改包括private权限的属性值)
@Test
public void testField() throws Exception{
String className = "com.aop.Person";
Class<?> clazz = Class.forName(className); Person person = (Person) clazz.newInstance(); //getField(...)方法会获取public类型的属性,包括父类的public类型属性都能获取
Field field = clazz.getField("name");
field.set(person, "东东");
System.out.println(person); //getDeclaredField(...)获取所有所有类型的属性,但是,不包括父类的属性在内
Field field2 = clazz.getDeclaredField("age");
//如果类中的属性设定为private或者是“默认类型”,则需要设置其可访问属性
field2.setAccessible(true);
field2.set(person, 100);
System.out.println(person); }
3、通过反射调用对象的方法
@Test
public void testMethod() throws Exception{
String className = "com.aop.Person";
Class<?> clazz = Class.forName(className); Person person = new Person(50, "哈哈"); //获取并调用无参的show方法
Method method1 = clazz.getMethod("show");
method1.invoke(person); //获取并调用有参的display方法
Method method2 = clazz.getMethod("display", String.class);
method2.invoke(person, "中国"); //调用有返回值的方法
Method method3 = clazz.getMethod("toString");
Object resultVal = method3.invoke(person);
System.out.println(resultVal); //调用有参数的静态方法
Method method4 = clazz.getMethod("countAdd", int.class, Integer.class);
method4.invoke(Person.class, 10, 100);
}
4、通过反射调用带参构造器来实例化一个对象
@Test
public void testConstructor() throws Exception{
String className = "com.aop.Person";
Class<?> clazz = Class.forName(className); //调用一个有参的构造器
Constructor<?> cons = clazz.getConstructor(int.class, String.class);
Person person = (Person) cons.newInstance(20, "兮兮");
System.out.println(person);
}
5、获取“父类”、“带泛型的父类”、“父类的泛型”
@Test
public void testGeneric() throws Exception{
String className = "com.aop.Person";
Class<?> clazz = Class.forName(className); //1、获取父类,只能得到父类的名称
Class<?> superClazz = clazz.getSuperclass();
System.out.println(superClazz); //2、获取带泛型的父类
Type type = clazz.getGenericSuperclass();
System.out.println(type); //3、获取父类的泛型类(重要)
ParameterizedType param = (ParameterizedType)type;
Type[] argmts = param.getActualTypeArguments();
Class<?> claz = (Class<?>)argmts[0];
System.out.println(claz);
}
下面是全部的源代码:
package com.aop; abstract public class Creature<T> { }
abstract public class Creature
package com.aop; public class Person extends Creature<String>{ int id;
public String name;
private int age; public Person() { }
public Person(int age, String name){
this.age = age;
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public int getAge() {
return age;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} static public void countAdd(int a, Integer b){
System.out.println(a + " + " + b + " = " + (a+b));
} public void show() {
System.out.println("我是一个人!");
} public void display(String nation) {
System.out.println("我的国际是:" + nation);
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
} }
public class Person extends Creature
package com.aop; import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import org.junit.Test; public class TestReflection { @Test
public void testReflection() throws Exception{ //1.调用运行时类本身的.class属性
Class<Person> clazz1 = Person.class; //2.通过运行时类的对象获取
Person person = new Person();
Class<? extends Person> clazz2 = person.getClass(); //3.通过Class的静态方法获取.通过此方式,体会一下,反射的动态性。
String className = "com.aop.Person"; Class<?> clazz3 = Class.forName(className); //4.(了解)通过类的加载器
ClassLoader loader = this.getClass().getClassLoader();
Class<?> clazz4 = loader.loadClass(className); System.out.println(clazz1.getName());
System.out.println(clazz2.getName());
System.out.println(clazz3.getName());
System.out.println(clazz4.getName()); System.out.println(clazz1 == clazz2);
System.out.println(clazz1 == clazz3);
System.out.println(clazz1 == clazz4);
} @Test
public void testField() throws Exception{
String className = "com.aop.Person";
Class<?> clazz = Class.forName(className); Person person = (Person) clazz.newInstance(); //getField(...)方法会获取public类型的属性,包括父类的public类型属性都能获取
Field field = clazz.getField("name");
field.set(person, "东东");
System.out.println(person); //getDeclaredField(...)获取所有所有类型的属性,但是,不包括父类的属性在内
Field field2 = clazz.getDeclaredField("age");
//如果类中的属性设定为private或者是“默认类型”,则需要设置其可访问属性
field2.setAccessible(true);
field2.set(person, 100);
System.out.println(person); } @Test
public void testMethod() throws Exception{
String className = "com.aop.Person";
Class<?> clazz = Class.forName(className); Person person = new Person(50, "哈哈"); //获取并调用无参的show方法
Method method1 = clazz.getMethod("show");
method1.invoke(person); //获取并调用有参的display方法
Method method2 = clazz.getMethod("display", String.class);
method2.invoke(person, "中国"); //调用有返回值的方法
Method method3 = clazz.getMethod("toString");
Object resultVal = method3.invoke(person);
System.out.println(resultVal); //调用有参数的静态方法
Method method4 = clazz.getMethod("countAdd", int.class, Integer.class);
method4.invoke(Person.class, 10, 100);
} @Test
public void testConstructor() throws Exception{
String className = "com.aop.Person";
Class<?> clazz = Class.forName(className); //调用一个有参的构造器
Constructor<?> cons = clazz.getConstructor(int.class, String.class);
Person person = (Person) cons.newInstance(20, "兮兮");
System.out.println(person);
} @Test
public void testGeneric() throws Exception{
String className = "com.aop.Person";
Class<?> clazz = Class.forName(className); //1、获取父类,只能得到父类的名称
Class<?> superClazz = clazz.getSuperclass();
System.out.println(superClazz); //2、获取带泛型的父类
Type type = clazz.getGenericSuperclass();
System.out.println(type); //3、获取父类的泛型类(重要)
ParameterizedType param = (ParameterizedType)type;
Type[] argmts = param.getActualTypeArguments();
Class<?> claz = (Class<?>)argmts[0];
System.out.println(claz);
} }
public class TestReflection
参考:尚硅谷java视频