反射获取对象的方法:
package reflect;
import java.lang.reflect.Constructor;
public class GetClassDemo1 {
public static void main(String[] args) throws Exception {
//通过全限定名获得类
/*Class<?> user = Class.forName("reflect.User");
User u = (User) user.newInstance();
u.print();*/
//通过类名获得
/* Class<User> userClass = User.class;
User user = userClass.newInstance();
user.print();*/
//通过实列化对象获得
// User user = new User();
// Class<? extends User> aClass = user.getClass();
// System.out.println(aClass);
//通过无参狗仔获取实列化对象
/*Class clazz = Class.forName("reflect.User");
Constructor constructor = clazz.getConstructor();
User user = (User) constructor.newInstance();
user.print();*/
//通过有参构造获取实列化对象
Class<?> clazz = Class.forName("reflect.User");
Constructor constructor = clazz.getConstructor(Integer.class,String.class,String.class);
User user = (User) constructor.newInstance(11,"z","nan");
System.out.println(user);
}
}
package reflect;
public interface China {
public static final String NATIONAL = "china";
public static final String AUTHOR = "gwj";
public void sayChina();
public String sayHello(String name, int id);
}
package reflect;
public class Person implements China{
private String name;
private Integer id;
public Person() {
}
public Person(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
@Override
public void sayChina() {
System.out.println("作者:"+AUTHOR+",国籍"+NATIONAL);
}
@Override
public String sayHello(String name, int age) {
return name+"你好!我今年"+age+"岁了!";
}
}
package reflect;
import java.lang.reflect.Method;
public class GetInterfaceDemo {
public static void main(String[] args) throws Exception {
Class clazz = Class.forName("reflect.Person");
//获得所有接口
/* Class[] interfaces = clazz.getInterfaces();
for(int i = 0; i < interfaces.length; i++){
System.out.println(interfaces[i].getName());
}*/
//获得所有构造方法
/*Constructor[] constructors = clazz.getConstructors();
for(int i = 0; i < constructors.length; i++){
System.out.println(constructors[i]);
}*/
//获得所有方法
/* Method[] methods = clazz.getMethods();
for(Method method : methods){
System.out.println(method.getTypeParameters());
}*/
//获得本类的属性
/* Field[] fields = clazz.getDeclaredFields();
for(Field f : fields){
System.out.println(f);
}*/
//获得父类的属性
/* System.out.println();
Field[] fields1 = clazz.getFields();
for(Field field : fields1){
System.out.println(field);
}*/
//调用方法
/*Object object = clazz.newInstance();
Method met1 = clazz.getMethod("sayChina");
met1.invoke(object);
Method met2 = clazz.getMethod("sayHello", String.class, int.class);
String str = (String) met2.invoke(clazz.newInstance(),"张三",12);
System.out.println(str);*/
//调用set和get方法
Object object = clazz.newInstance();
setter(object,"name","张三",String.class);
setter(object,"id",121,int.class);
getter(object,"name");
getter(object,"id");
}
public static void setter(Object obj,String att,Object value,Class<?> type) throws Exception {
Method method = obj.getClass().getMethod("set" + initStr(att), type);
method.invoke(obj,value);
}
public static void getter(Object obj,String att) throws Exception {
Method method = obj.getClass().getMethod("get"+initStr(att));
System.out.println(method.invoke(obj));
}
public static String initStr(String old){
String str = old.substring(0,1).toUpperCase()+old.substring(1);
return str;
}
}
反射在框架中用的非常多, 写个案列只改变配置文件 不改变原框架代码来创建任意对象执行任意方法
package reflect.demo;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* @BelongsProject: day_2
* @BelongsPackage: reflect.demo
* @Author: gwj
* @CreateTime: 2020-05-05 16:19
* @Description: 写一个框架在不改变该类的任何代码的前提下,可以帮我们创建任意类的对象
* 并且执行其中的任意方法
*/
public class ReflectTest {
/**
* 通过配置文件 和反射实现 只需要更改配置文件
* @param args
*/
public static void main(String[] args) throws Exception {
//加载配置问件
//创建Properties 对象
Properties properties = new Properties();
//加载配置文件,转化为一个集合
//获取class目路下的配置文件
ClassLoader loader = ReflectTest.class.getClassLoader();
InputStream resourceAsStream = loader.getResourceAsStream("pro.properties");
properties.load(resourceAsStream);
//获取配置文件中的数据
String className = properties.getProperty("className");
String methodName = properties.getProperty("methodName");
//加载该类进内存
Class<?> aClass = Class.forName(className);
//创建对象
Object o = aClass.newInstance();
//获取方法对象
Method method = aClass.getMethod(methodName);
method.invoke(o);
}
}
package reflect.demo;
/**
* @BelongsProject: day_2
* @BelongsPackage: reflect.demo
* @Author: gwj
* @CreateTime: 2020-05-05 16:16
* @Description: todo
*/
public class Student {
public void sleep(){
System.out.println("sleep....");
}
}
className=reflect.demo.Student
methodName=sleep