反射的用法: 1)、需要获得java类的各个组成部分,首先需要获得类的Class对象,获得Class对象的三种方式: Class.forName(classname) 用于做类加载 obj.getClass() 用于获得对象的类型 类名.class 用于获得指定的类型,传参用 2)、反射类的构造函数: Constructorcon = clazz.getConstructor(new Class[]{paramClazz1, paramClazz2,...}) con.newInstance(params...) 3)、反射类的成员方法: Classclazz = Person.class; Methodmethod = clazz.getMethod(methodName, new Class[]{paramClazz1, paramClazz2}); method.invoke(); 4)、反射类的属性: Fieldfield = clazz.getField(fieldName); field.setAccessible(true); field.setObject(value);
- import java.lang.reflect.Constructor;
- import java.lang.reflect.*;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- public class test1 {
- public static void main(String[] args) throws Exception {
- mether1();
- mether2();
- mether3();
- mether4();
- }
-
- //获取字节码class
- public static void mether1() throws Exception{
- // 1. 根据给定的类名来获得 用于类加载
- String classname = "javaenhance.Person";// 一般来自配置文件
- Class c1 = Class.forName(classname);// 此对象代表Person.class
- // 2. 如果拿到了对象,不知道是什么类型 用于获得对象的类型
- Object obj = new Person();
- Class c2 = obj.getClass();// 获得对象具体的类型
-
- // 3. 如果是明确地获得某个类的Class对象 主要用于传参
- Class c3 = Person.class;
-
- System.out.println(c1);
- System.out.println(c2);
- System.out.println(c3);
- System.out.println("获取获取字节码class");
- System.out.println();
- }
- //实例化对象
- private static void mether2() throws Exception {
- Class c = Person.class;
- Constructor co = c.getDeclaredConstructor();
- Person p = (Person) co.newInstance();
-
- Constructor co1 = c.getDeclaredConstructor(String.class,int.class);
- Person p1 = (Person) co1.newInstance("sharry",11);
-
- System.out.println(p.toString());
- System.out.println(p1.toString());
-
- System.out.println("实例化对象");
- System.out.println();
- }
-
- //调用成员方法
- private static void mether3() throws Exception{
- // TODO 自动生成的方法存根
- Class c = Person.class;
- Constructor co1 = c.getDeclaredConstructor(String.class,int.class);
- Person p1 = (Person) co1.newInstance("sharry",11);
-
- System.out.println(p1.toString());
-
- Method method = c.getMethod("setName", String.class);
- method.invoke(p1, "make");
-
- System.out.println(p1.toString());
-
- System.out.println("调用成员方法");
- System.out.println();
- }
-
- //获取属性
- private static void mether4(){
- Class<?> demo = null;
- try {
- demo = Person.class;
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 取得本类的全部属性
- Field[] field = demo.getDeclaredFields();
- for (int i = 0; i < field.length; i++) {
- // 权限修饰符
- int mo = field[i].getModifiers();
- String priv = Modifier.toString(mo);
- // 属性类型
- Class<?> type = field[i].getType();
- System.out.println(priv + " " + type.getName() + " "
- + field[i].getName() + ";");
- }
- System.out.println("获取属性");
- System.out.println();
- }
- }
- class Person {
-
- private String name;
- private int age;
-
- public Person() {
- }
- public Person(String name, int age) {
-
- this.name = name;
- this.age = age;
- }
-
- public String toString(){
- return "name:"+name+" age:"+age;
- }
-
- public String sayHello(int age,String name){
- return "My name is "+name+","+age+"years old ,nice to meet you ";
- }
-
- private void myPrivate(){
- System.out.println("i am private");
- }
-
- 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;
- }
-
- }
复制代码
|