码迷,mamicode.com
首页 > 其他好文 > 详细

Reflect

时间:2016-05-16 12:56:33      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

反射学习笔记

//测试类

public class Person {

  public String name = "HelloWorld";
  private int age;

  public Person(String name) {
    System.out.println("public Person(String name) constructor...");
  }

  private Person(int age) {
    System.out.println("private Person(int age) constructor...");
  }

  public Person() {

    System.out.println("public Person() constructor...");
  }

  public static void main(String[] args) {
    System.out.println("main run...");
  }

}

  

 

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by xing on 2016/5/15.
 */
public class Reflect { public static void main(String[] args) throws Exception { /* * 反射:加载类解剖类的各个组成成分 * */ // accessClass(); // accessConstructor(); // accessMethod(); accessField(); } private static void accessField() throws Exception { /* * 获取字段 * * 1.获取字节码对象 * * 2.调用字节码对象的getFiled()方法 ; 参数为字段名 * public Field getField(String name) throws NoSuchFieldException, SecurityException 3.调用Field的get()方法获取字段值 public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException ps:Field的getType()方法可以获取字段类型 * */ Class clazz = Class.forName("Person"); //获取字段 Field field = clazz.getField("name"); //获取字段值 String value=(String) field.get(new Person()); System.out.println(value); } private static void accessMethod() throws Exception { /* * 获取方法 * * 1.获取字节码对象 * * 2.调用字节码对象的getMethod()方法; name.equals(方法名); parameterTypes参数类型的字节码对象 * public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException * 3.调用Method类的invoke(); obj就是调用此方法的对象 * public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException * * */ //获取main函数 Person p = new Person(); Class clazz = Class.forName("Person"); Method method=clazz.getMethod("main",String[].class); method.invoke(p,new Object[]{new String[]{"Hello","World"}}); } private static void accessConstructor() throws Exception { /* * 获取构造函数 * * 1.获取字节码对象 * * 2.调用字节码对象的getConstructor()方法; public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException,SecurityException * 3.通过Constructor的newInstance()方法构造相对应的实例 * public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException ps: setAccessible(true)可以用来访问私有成员。(暴力反射) 而且当访问私有成员时调用的是getDeclared*()的函数; * */ Class clazz = Class.forName("Person"); Constructor con=clazz.getDeclaredConstructor(String.class); Person p=(Person)con.newInstance(); /* * 访问私有构造函数private Person(int age) * Class clazz = Class.forName("Person"); * * Constructor con=clazz.getDeclaredConstructor(int.class); * * con.setAccessible(true); * * Person p=(Person)con.newInstance(24); * */ } private static void accessClass() throws Exception { /* * 获取字节码对象的方式 * 1.public static Class<?> forName(String className) throws ClassNotFoundException 2.对象.getclass(); Person p = new Person(); Class clazz=p.getClass(); 3.类名.class Class clazz=Person.class;或 int.class void.class... * */ Class clazz = Class.forName("Person"); System.out.println(clazz); /* Person p = new Person(); Class clazz=p.getClass(); System.out.println(clazz);*/ /* Class clazz=Person.class; System.out.println(clazz);*/ } }

  

Reflect

标签:

原文地址:http://www.cnblogs.com/programmerzzx/p/5497661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!