zoukankan      html  css  js  c++  java
  • Java反射常用示例

    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+" getSimpleName:"+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+" field.getName:"+field.getName()+" field.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

     
  • 相关阅读:
    RocketMQ源码分析:(二)消息发送的三种方式
    LTS本地搭建详述
    Mac端解决(含修改8.0.13版的密码):Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    flink入门:01 构建简单运行程序
    rocketmq控制台搭建(rocketmq-console)
    在consul上注册web服务
    将filenames里的每个字符串输出到out文件对象中注意行首的缩进
    spidermark sensepostdata ntp_monlist.py
    HTTP Error 403没有了,但是中文全都是乱码。又是怎么回事?
    original.txt和提交的页面输出的文字的混合文件
  • 原文地址:https://www.cnblogs.com/xmqa/p/8796859.html
Copyright © 2011-2022 走看看