package com.hspedu.reflection.question;
import com.hspedu.Cat;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* @version 1.0
* @auter liyang
* 反射问题
*/
@SuppressWarnings({"all"})
public class ReflectionQuestion {
public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Cat cat=new Cat();
cat.hi();
Properties properties=new Properties();
properties.load(new FileInputStream("src\\re.properties"));
String classfullpath=properties.get("classfullpath").toString();
String methodName = properties.get("method").toString();
System.out.println("classfullpath"+classfullpath);
System.out.println("method"+methodName);
Class cls=Class.forName(classfullpath);
//加载cat类
Object o = cls.newInstance();
System.out.println("o的运行类型"+o.getClass());
Method method1 = cls.getMethod(methodName);
method1.invoke(o);
}
}
配置文件
classfullpath=com.hspedu.Cat
method=cry
类方法
package com.hspedu;
/**
* @version 1.0
* @auter liyang
*/
public class Cat {
private String name="招财猫";
public void hi(){
System.out.println("hi" +name);
}
public void cry(){
System.out.println("cry.........");
}
}