package xmq.study.reflection;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier; /***
* Java 反射
* @author 943567518@qq.com
*
*/
public class Test1 { private String prop1;
private Integer prop2;
   private Double  prop3; public String getProp1() {
return prop1;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
public Integer getProp2() {
return prop2;
}
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}

  public Double getProp3() {
    return prop3;
  }
  public void setProp3(Double prop3) {
    this.prop3 = prop3;
  }


    public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException {

        Test1 test1=new Test1();
test1.setProp1("Hello");
test1.setProp2(100); Class clazz=Test1.class;//获取Class对象方式1
Class clazz2=Class.forName("xmq.study.reflection.Test1");//获取Class对象方式2 String clazzName=clazz.getName();//获取类名,含包名
String clazzSimpleName=clazz.getSimpleName();//获取类名,不含包名
System.out.println("getName:"+clazzName+"\tgetSimpleName:"+clazzSimpleName); int mod=clazz.getModifiers();//获取类修饰符
System.out.println("Modifier.isPublic:"+Modifier.isPublic(mod));//判断类修饰符
System.out.println("Modifier.isProtected:"+Modifier.isProtected(mod));//判断类修饰符 Package p=clazz.getPackage();//获取包
System.out.println("getPackage:"+p); Class superClass=clazz.getSuperclass();//获取父类
System.out.println("getSuperclass:"+superClass); Class[] interfaces=clazz.getInterfaces();//获取实现接口
System.out.println("getInterfaces:"+interfaces.length); Constructor[] cons=clazz.getConstructors();//构造方法
System.out.println("getConstructors:"+cons.length);
     
      

      Method[] methods=clazz.getMethods();//获取所有方法
      System.out.println("getMethods:"+methods.length);
      for(Method method:methods){
        System.out.println("method.getName:"+method);
      }

        Method[] methods=clazz.getMethods();//获取私有方法
System.out.println("getMethods:"+methods.length);
for(Method method:methods){
System.out.println("method.getName:"+method.getName());
}

      Method method=clazz.getMethod("getProp1", null);//获取指定方法
      System.out.println("getMethod(,):"+method);
      Object methodVlaue=method.invoke(test1, null);//调用方法
      System.out.println("method.invoke(,):"+methodVlaue);

      Method method3=clazz.getMethod("setProp3",Double.class);//获取指定方法
      System.out.println("getMethod(,):"+method3);
      Object methodVlaue3=method3.invoke(test1, 2.0);//调用setter方法,该方法没有返回值,所以methodVlaue3为null;此处注意参数2.0 ,不能用null
      System.out.println("method.invoke(,):"+methodVlaue3);

        Field[] fields=clazz.getDeclaredFields();//获取变量
System.out.println("getDeclaredFields:"+fields.length);
for(Field field:fields){
field.setAccessible(true);
field.set(test1,null);//设置字段的值
System.out.println("field.getAnnotations:"+field.getAnnotations().length+"\tfield.getName:"+field.getName()+"\tfield.get:"+field.get(test1));//获取实例属性名和值 } Annotation[] annos=clazz.getAnnotations();//获取类注解
System.out.println("getAnnotations:"+annos.length); }
}
控制台输出:

getName:xmq.study.reflection.Test1 getSimpleName:Test1
Modifier.isPublic:true
Modifier.isProtected:false
getPackage:package xmq.study.reflection
getSuperclass:class java.lang.Object
getInterfaces:0
getConstructors:1
getMethods:16
method.getName:public static void xmq.study.reflection.Test1.main(java.lang.String[]) throws java.lang.ClassNotFoundException,java.lang.IllegalArgumentException,java.lang.IllegalAccessException,java.lang.NoSuchMethodException,java.lang.SecurityException,java.lang.reflect.InvocationTargetException
method.getName:public java.lang.String xmq.study.reflection.Test1.getProp1()
method.getName:public void xmq.study.reflection.Test1.setProp1(java.lang.String)
method.getName:public java.lang.Integer xmq.study.reflection.Test1.getProp2()
method.getName:public void xmq.study.reflection.Test1.setProp2(java.lang.Integer)
method.getName:public java.lang.Double xmq.study.reflection.Test1.getProp3()
method.getName:public void xmq.study.reflection.Test1.setProp3(java.lang.Double)
method.getName:public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
method.getName:public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
method.getName:public final void java.lang.Object.wait() throws java.lang.InterruptedException
method.getName:public boolean java.lang.Object.equals(java.lang.Object)
method.getName:public java.lang.String java.lang.Object.toString()
method.getName:public native int java.lang.Object.hashCode()
method.getName:public final native java.lang.Class java.lang.Object.getClass()
method.getName:public final native void java.lang.Object.notify()
method.getName:public final native void java.lang.Object.notifyAll()
getDeclaredMethods:7
method.getName:public static void xmq.study.reflection.Test1.main(java.lang.String[]) throws java.lang.ClassNotFoundException,java.lang.IllegalArgumentException,java.lang.IllegalAccessException,java.lang.NoSuchMethodException,java.lang.SecurityException,java.lang.reflect.InvocationTargetException
method.getName:public java.lang.String xmq.study.reflection.Test1.getProp1()
method.getName:public void xmq.study.reflection.Test1.setProp1(java.lang.String)
method.getName:public java.lang.Integer xmq.study.reflection.Test1.getProp2()
method.getName:public void xmq.study.reflection.Test1.setProp2(java.lang.Integer)
method.getName:public java.lang.Double xmq.study.reflection.Test1.getProp3()
method.getName:public void xmq.study.reflection.Test1.setProp3(java.lang.Double)
getMethod(,):public java.lang.String xmq.study.reflection.Test1.getProp1()
method.invoke(,):Hello
getMethod(,):public void xmq.study.reflection.Test1.setProp3(java.lang.Double)
method.invoke(,):null
getDeclaredFields:3
field.getAnnotations:0 field.getName:prop1 field.get:null
field.getAnnotations:0 field.getName:prop2 field.get:null
field.getAnnotations:0 field.getName:prop3 field.get:null
getAnnotations:0

 

最新文章

  1. Delphi 控件大全
  2. Web API应用架构在Winform混合框架中的应用(5)--系统级别字典和公司级别字典并存的处理方式
  3. draw call 的优化
  4. shake震动动画
  5. C++同步串口通信
  6. win8系统中PL/SQL Developer连接Oracle出现的问题
  7. nyoj 96 n-1位数(处理前导 0 的情况)
  8. 在LINUX中跟踪函数调用----http://stackoverflow.com/
  9. ZendFramework 环境部署
  10. Swift 细节
  11. 包的初识和进阶&异常处理
  12. linux 部分系统命令
  13. Java并发编程:volatile关键字解析-转
  14. Java-Runoob-高级教程-实例-方法:15. Java 实例 – 重载(overloading)方法中使用 Varargs
  15. linux 内核根文件系统
  16. 3143 二叉树的序遍历codevs
  17. attack source code
  18. [Apio2012]dispatching 主席树做法
  19. Codeforces 1006C:Three Parts of the Array(前缀和+map)
  20. IndexedDB:浏览器里内置的数据库(转)

热门文章

  1. 微信小程序开发系列一:微信小程序的申请和开发环境的搭建
  2. Android(java)学习笔记175:Android进程间通讯(IPC)之AIDL
  3. Kubernetes 架构(上)【转】
  4. 按名字寻找文件和文件夹 find命令
  5. python基础:函数传参、全局变量、局部变量、内置函数、匿名函数、递归、os模块、time模块
  6. 【简●解】[SDOI2008] Sue的小球
  7. luogu P1008 三连击
  8. selenium——操作滚动条
  9. 如何用纯 CSS 创作一个荧光脉冲 loader 特效
  10. python_random模块