Java 随心笔记11

获取无参构造方法

Class c = Class.forname("包名");
Constructor[] cons = c.getConstructors();//复数
Constructor con = c.getConstructors();
Obgect obj = con.newInstance();

获取有参构造

Class c = Class.forname("包名");
//获取带有String和int参数的构造方法
Constructor con = c.getConstructor(String.class,int.class);
//运行构造方法
Object obj = con.newInstance("张三",33);
System.out.print(obj);

获取构造方法并运行的快速的方式

前提:被反射的类,必须具有空参数构造方法 构造方法权限必须public

Class c = Class.forname("包名");
//T newInstance()运行获取构造器方法
Object obj = c.newInstance();
System.out.pritnln(obj);

获取构造方法并运行

Class c = Class.forname("包名");
Constructor con = c.getDeclaredConstructor(int.class,String.class)//指定列表的构造方法
con.setAccessible(true);//暴力反射
Object obj = con.newInstance(22,"张");

获取成员变量

Class c = Class.forname("包名");
//获取成员变量class类的方法的公共的成员变量
//返回值是Field[]
Field[] f = c.getFields();//字段
for(Field s:f){
  System.out.println(s);
}
//获取指定的成员变量String name
Field field = c.getField("name");
//修改成员变量的值
Object obj必须要有对象的支持
Object obj = c.newInstance();
field.set(obj,"王五");

获取成员方法/空参

Class c = Class.forname("包名");
//获取class对象的成员方法
//getMethods()获取class文件中素有的公共成员方法,包括继承的
Method[] methods = c.getMethods();

//获取指定的方法run运行
//Method getMethod(String methodName,Class c);
Method method = c.getMethod("run",);
//使用Method类中的方法,运行获取到的方法run
//Object invoke()
Object obj = c.newInstance();
method.invoke(obj);

获取成员方法有参


Class c = Class.forname("包名");
Object obj = c.newInstance();
//调用Class类的
c.getMethod("sleep",String.class,int.class,double.class);
method.invoke(obj,"休眠",99,10.33);

泛型擦除

//定义集合类,泛型String

//要求向集合中添加Integer类型

反射方式,获取出集合ArrayList类的class文件对象

通过class文件对象,调用add方法


ArrayList<String>array = new ArrayList<String>();
array.add("a");

Class c = array.getClass();
Method method = c.getMethod("add",Object.class);
method.invoke(array,15);
System.our.println(array);

反射通过配置文件运行的步骤

package com.properties;
​
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Properties;
​
public class Test {
    public static void main(String[] args)throws Exception {
        FileReader r = new FileReader("config.properties");
        Properties pro = new Properties();
        pro.load(r);
        r.close();//获取类名方法名
        String className = pro.getProperty("className");
        String methodName = pro.getProperty("methodName");
        //class文件对象
        Class c = Class.forName(className);
        Object obj = c.newInstance();
        //获取指定的方法名
        Method method = c.getMethod(methodName);
        method.invoke(obj);
        
    }
}
1
package com.properties;
​
public class Person {
    public void eat() {
        System.out.println("人在吃饭");
    }
}
2
package com.properties;
​
public class Student {
    public void study() {
        System.out.println("学生再吃饭");
    }
}
3
package com.properties;
​
public class Worker {
    public void job() {
        System.out.println("在工作");
    }
}
4 config.properties
className=com.properties.Person
methodName=eat
同样的梦想,不一样的起点,大家共同进步, 微信公众号「孝客之乡」作者。
原文地址:https://www.cnblogs.com/JulyTail/p/9170612.html