反射快速入门
package reflection;
public class cat {
private String name="招财猫";
public int age=10;
public String goa="jack";
public double salary=100;
public cat(){}
public cat(String name) {
this.name = name;
}
public void hi(){
}
public void cry(){
System.out.println("喵喵叫"+name);
}
@Override
public String toString() {
return "cat{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package reflection;
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;
public class reflection {
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Properties properties=new Properties();
properties.load(new FileInputStream("src\\hello.txt"));
String classfullpath=properties.get("classfullpath").toString();
String method=properties.get("method").toString();
System.out.println(classfullpath);
System.out.println(method);
Class cls=Class.forName(classfullpath);
Object o=cls.newInstance();
System.out.println(o.getClass());
Method method1= cls.getMethod(method);
method1.invoke(o);
}
}
反射与直接调用速度
package reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class reflection1 {
public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
m1();
m2();
}
public static void m1(){
cat cat=new cat();
long start=System.currentTimeMillis();
for(int i=0;i<900000;i++){
cat.hi();
}
long end=System.currentTimeMillis();
System.out.println(end-start);
}
public static void m2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class cls=Class.forName("reflection.cat");
Object o=cls.newInstance();
Method method=cls.getMethod("hi");
method.setAccessible(true);
long start=System.currentTimeMillis();
for(int i=0;i<900000;i++){
method.invoke(o);
}
long end=System.currentTimeMillis();
System.out.println(end-start);
}
}
反射方法基本调用
package reflection;
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;
public class reflection {
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Properties properties=new Properties();
properties.load(new FileInputStream("src\\hello.txt"));
String classfullpath=properties.get("classfullpath").toString();
String method=properties.get("method").toString();
System.out.println(classfullpath);
System.out.println(method);
Class cls=Class.forName(classfullpath);
Object o=cls.newInstance();
System.out.println(o.getClass());
Method method1= cls.getMethod(method);
method1.invoke(o);
}
}