反射随笔
package ff.learn.w08;
public class Test01 {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
//通过对象获得
Class c1 = person.getClass();
System.out.println(c1.hashCode());
//forName()
Class<?> c2 = Class.forName("ff.learn.w08.Student");
System.out.println(c2.hashCode());
//类名.class
Class<Student> c3 = Student.class;
System.out.println(c3.hashCode());
//基本内置类型包装类 TYPE属性
Class<Integer> type = Integer.TYPE;
System.out.println(type);
//获得父类类型
Class c5 = c1.getSuperclass();
System.out.println(c5.hashCode());
}
}
class Person{
public String name;
public int age;
private String psw;
public Person() {
}
public Person(String name, int age, String psw) {
this.name = name;
this.age = age;
this.psw = psw;
}
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;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", psw='" + psw + '\'' +
'}';
}
}
class Student extends Person{
public Student(){
this.name = "学生";
}
}
class Teacher extends Person{
public Teacher(){
this.name = "老师";
}
}
package ff.learn.w08;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class<?> c1 = Class.forName("ff.learn.w08.Person");
//获得类的名字
System.out.println(c1.getName());//获得包名+类名
System.out.println(c1.getSimpleName());//获得类名
//获得类的属性
System.out.println("------------------------------------------");
Field[] fields = c1.getFields();//只能获得public属性
for (Field field : fields) {
System.out.println(field);
}
System.out.println("------------------------------------------");
fields = c1.getDeclaredFields();//获得全部属性
for (Field field : fields) {
System.out.println(field);
}
System.out.println("------------------------------------------");
//获得指定属性
// Field psw = c1.getField("psw"); //不能获得私有的
//System.out.println(psw);
Field psw = c1.getDeclaredField("psw");
System.out.println(psw);
System.out.println("------------------------------------------");
//获得类的方法
Method[] methods = c1.getMethods();//获得本类和父类全部public方法
for (Method method : methods) {
System.out.println(method);
}
System.out.println("------------------------------------------");
methods = c1.getDeclaredMethods();//获得本类和父类全部方法
for (Method method : methods) {
System.out.println(method);
}
System.out.println("------------------------------------------");
//获得指定方法
//重载
Method getName = c1.getMethod("getName", null);
Method setName = c1.getMethod("setName", String.class);
System.out.println(getName);
System.out.println(setName);
//获得指定构造器
System.out.println("------------------------------------------");
Constructor<?>[] constructors = c1.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
System.out.println("------------------------------------------");
constructors = c1.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
Constructor<?> constructor = c1.getConstructor(String.class, int.class, String.class);
System.out.println(constructor);
}
}
package ff.learn.w08;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test03 {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class<?> c1 = Class.forName("ff.learn.w08.Person");
/*Person o = (Person) c1.newInstance();
System.out.println(o);*/
/*Constructor constructor= c1.getDeclaredConstructor(String.class,int.class,String.class);
Person person = (Person) constructor.newInstance("王", 001, "123");
System.out.println(person);*/
//通过反射调用方法
Person o = (Person) c1.newInstance();
//通过反射获取方法
Method setName = c1.getDeclaredMethod("setName", String.class);
//invoke 激活的意思
setName.invoke(o,"大腚");
System.out.println(o.getName());
//通过方式操作属性
Person o1 = (Person) c1.newInstance();
Field name = c1.getDeclaredField("name");
//不能直接操作私有属性
name.setAccessible(true);//关掉私有程序的安全检测
name.set(o1,"张大腚");
System.out.println(o1.getName());
}
}
分析性能
package ff.learn.w08;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//分析性能
public class Test04 {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
demo();
demo2();
demo3();
}
//普通方式调用
public static void demo() {
Person person = new Person();
long starTime = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
person.getName();
}
long endTime = System.currentTimeMillis();
System.out.println("普通方式执行10亿次:" + (endTime - starTime) + "ms");
}
//反射方式调用
public static void demo2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Person person = new Person();
Class<? extends Person> c1 = person.getClass();
Method getName = c1.getDeclaredMethod("getName",null);
long starTime = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
getName.invoke(person,null);
}
long endTime = System.currentTimeMillis();
System.out.println("反射方式执行10亿次:"+(endTime-starTime)+"ms");
}
//关闭检测
public static void demo3() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Person person = new Person();
Class<? extends Person> c1 = person.getClass();
Method getName = c1.getDeclaredMethod("getName",null);
getName.setAccessible(true);
long starTime = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
getName.invoke(person,null);
}
long endTime = System.currentTimeMillis();
System.out.println("关闭检测方式执行10亿次:"+(endTime-starTime)+"ms");
}
}
执行结果