2-1-2-07-08-09 Class对象功能描述及获取
Class对象功能:
获取功能:
1.获取成员变量们
成员变量操作:
1.设置值
set(Object obj, Object value)
2.获取值
get(Object obj)
3,忽略访问权限修饰符的安全检查,来设置private的值
setAccessible(true)
实体类
public class ReflectEntity { private String name; private int age; public String a; public String b; public String c; public String d; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "ReflectEntity{" + "name='" + name + '\'' + ", age=" + age + ", a='" + a + '\'' + ", b='" + b + '\'' + ", c='" + c + '\'' + ", d='" + d + '\'' + '}'; } public ReflectEntity(String name, int age) { this.name = name; this.age = age; } public ReflectEntity() { } private ReflectEntity(String a, String b, String c, String d) { this.a = a; this.b = b; this.c = c; this.d = d; } private ReflectEntity(String a) { this.a = a; } public void eat(){ System.out.println("吃"); } public void eat(String name){ System.out.println("吃"+name); } private void eat(String name, int time){ System.out.println(time+"点"+"吃"+name); } }
获取类对象
//获取类对象 Class aClass = ReflectEntity.class;
Field[ ] getFields():获取所有的public修饰的成员变量
//1.Field[] getFields() Field[] fields = aClass.getFields(); for (Field field : fields) { System.out.println(field); } System.out.println("---------------");
field getField(String name):获取指定的public修饰的成员变量
//2.field getField(String name) Field a = aClass.getField("a"); //获取成员变量a的值 ReflectEntity reflectEntity = new ReflectEntity(); Object o = a.get(reflectEntity); System.out.println(o);//获取的值为null //设置成员变量a的值,要传两个值,一个是该对象,另一个是该成员变量要设置的值 a.set(reflectEntity,"张三"); System.out.println(reflectEntity); System.out.println("---------------");
Field[ ] getDeclaredFields():获取所有的成员变量(包括私有成员变量)
//3.Field[] getDeclaredFields() Field[] declaredFields = aClass.getDeclaredFields(); for (Field declaredField : declaredFields) { System.out.println(declaredField); } System.out.println("---------------");
Field getDeclaredField(String name):获取指定的成员变量(包括私有成员变量,但是设置会报错,如果要对私有的成员变量设置不报错,则要 成员变量.setAccessible(true))
//4.Field getDeclaredField(String name) Field name = aClass.getDeclaredField("name"); System.out.println(name); //忽略访问权限修饰符的安全检查 ReflectEntity reflectEntity2 = new ReflectEntity(); name.setAccessible(true); Object o1 = name.get(reflectEntity2); System.out.println(o1); //设置成员变量name的值,要传两个值,一个是该对象,另一个是该成员变量要设置的值 name.set(reflectEntity2,"李四"); System.out.println(reflectEntity2);
2.获取构造方法们
创建对象:
T newInstance(Object...initargs)
如果使用空参数构造方法创建对象,操作可以简化:Class对象的newInstance方法
忽略访问权限修饰符的安全检查,来设置private的构造器值:setAccessible(true)
实体类
public class ReflectEntity { private String name; private int age; public String a; public String b; public String c; public String d; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "ReflectEntity{" + "name='" + name + '\'' + ", age=" + age + ", a='" + a + '\'' + ", b='" + b + '\'' + ", c='" + c + '\'' + ", d='" + d + '\'' + '}'; } public ReflectEntity(String name, int age) { this.name = name; this.age = age; } public ReflectEntity() { } private ReflectEntity(String a, String b, String c, String d) { this.a = a; this.b = b; this.c = c; this.d = d; } private ReflectEntity(String a) { this.a = a; } public void eat(){ System.out.println("吃"); } public void eat(String name){ System.out.println("吃"+name); } private void eat(String name, int time){ System.out.println(time+"点"+"吃"+name); } }
获取类对象
//获取类对象 Class aClass = ReflectEntity.class;
Constructor<?>[ ] getConstructors():获取所有的public构造器方法
//1.aClass.getConstructors(); Constructor[] constructors = aClass.getConstructors(); for (Constructor constructor : constructors) { System.out.println(constructor); } System.out.println("1---------------");
Constructor<T> getConstructor(类<?>......parameterType):获取指定的public构造器方法
// 2.aClass.getConstructor(); Constructor constructor = aClass.getConstructor(String.class, int.class); Object o2 = constructor.newInstance("张三", 23); System.out.println(o2); System.out.println("2---------------");
Constructor<T> getDeclaredConstructor(类<?>......parameterType):获取所有的构造器方法
// 3.aClass.getDeclaredConstructors(); Constructor[] declaredConstructors = aClass.getDeclaredConstructors(); for (Constructor declaredConstructor : declaredConstructors) { System.out.println(declaredConstructor); } System.out.println("3---------------");
Constructor<?>[ ] getDeclaredConstructors():获取指定的构造器方法
// 4.aClass.getDeclaredConstructor(); Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class, String.class, String.class, String.class); declaredConstructor.setAccessible(true); Object o3 = declaredConstructor.newInstance("库尔勒市", "巴州", "新疆", "中国"); System.out.println(o3); System.out.println("4---------------");
Constructor<?>[ ] getDeclaredConstructors():获取指定的构造器方法,获取参数为空的构造器
//空参构造器:普通方法 Constructor declaredConstructor1 = aClass.getDeclaredConstructor(); Object o4 = declaredConstructor1.newInstance(); System.out.println(o4); //空参构造器:简单方法 Object o5 = aClass.newInstance(); System.out.println(o5);
3.获取成员方法们
执行方法:
Object invoke(Object obj, Object...args):传的参数分别是方法名和方法的参数
获取方法名称:
Srting getName
实体类
public class ReflectEntity { private String name; private int age; public String a; public String b; public String c; public String d; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "ReflectEntity{" + "name='" + name + '\'' + ", age=" + age + ", a='" + a + '\'' + ", b='" + b + '\'' + ", c='" + c + '\'' + ", d='" + d + '\'' + '}'; } public ReflectEntity(String name, int age) { this.name = name; this.age = age; } public ReflectEntity() { } private ReflectEntity(String a, String b, String c, String d) { this.a = a; this.b = b; this.c = c; this.d = d; } private ReflectEntity(String a) { this.a = a; } public void eat(){ System.out.println("吃"); } public void eat(String name){ System.out.println("吃"+name); } private void eat(String name, int time){ System.out.println(time+"点"+"吃"+name); } }
创建类对象
//获取类对象 Class aClass = ReflectEntity.class;
Method[ ] getMethods():获取所有public方法,包括所有类的父类Object类中的方法
//1.Method[ ] getMethods() Method[] methods = aClass.getMethods(); for (Method method : methods) { System.out.println(method); String name1 = method.getName(); System.out.println(name1); }
Method<T> getMethod(String name, 类<?>......parameterType):获取指定的public方法
//2.Method<T> getMethod(String name, 类<?>......parameterType) ReflectEntity reflectEntity1 = new ReflectEntity(); //空参方法 Method methods1 = aClass.getMethod("eat"); methods1.invoke(reflectEntity1); //有参数的方法 Method method2 = aClass.getMethod("eat", String.class); method2.invoke(reflectEntity1,"面条");
Method[ ] getDeclaredMethods():获取所有方法,包括所有类的父类Object类中的方法
//3.Method[ ] getDeclaredMethods() Method[] declaredMethods = aClass.getDeclaredMethods(); for (Method declaredMethod : declaredMethods) { System.out.println(declaredMethod); String name1 = declaredMethod.getName(); System.out.println(name1); }
Method<T> getDeclaredMethod(String name, 类<?>......parameterType):获取指定的方法
//4.Method<T> getDeclaredMethod(String name, 类<?>......parameterType) ReflectEntity reflectEntity3 = new ReflectEntity(); Method eat = aClass.getDeclaredMethod("eat", String.class, int.class); eat.setAccessible(true); eat.invoke(reflectEntity3,"米饭",3);
4.获取类名
String getName()
//获取类的全路径名 String name1 = aClass.getName(); System.out.println(name1);