美文网首页
java 反射

java 反射

作者: 第八共同体 | 来源:发表于2019-07-12 16:19 被阅读0次
public static void main(String[] args) throws Exception {
        System.out.println("main method");
        
        
        
        Object obj = 1;
        Object obj1 = 1;
        System.out.println("1".equals(obj1)); // false
        System.out.println(obj.equals(obj1)); // true
        System.out.println(obj.toString());
    }

查其原因:

/**
     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     *
     * @param  anObject
     *         The object to compare this {@code String} against
     *
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     *
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
     */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

关于字符串的equals方法,结果返回true,当且仅当参数非空,并且是一个String对象,代表相同的字节序列。

1.示例

委托类

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package demoGradleOne;

public class Library {
    
    private String name;
    
    
    
    public Library() {
        super();
    }

    public Library(String name) {
        super();
        this.setName(name);
    }

    public boolean someLibraryMethod() {
        return true;
    }
    
    @Override
    public String toString() {
        return "demoGradleOne";
    }
    
    private String write() {
        return "write method";
    }
    
    public static void main(String[] args) throws Exception {
        System.out.println("main method");
        
        
        
        Object obj = 1;
        Object obj1 = 1;
        System.out.println("1".equals(obj1));
        System.out.println(obj.toString());
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


测试

package demoGradleOne;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;


public class App {

    public static void main(String[] args) throws Exception {
        
        Library lib = new Library();
        Class<? extends Library> cl = lib.getClass();
        Class<Library> cl2 = Library.class;
        System.out.println(cl.getName());
        System.out.println(cl == cl2);
        
        Class cl3;
        try {
            cl3 = Class.forName("demoGradleOne.Library");
            Constructor[] conArray = cl3.getConstructors(); 
            for(Constructor c : conArray){ 
                if (c.getParameterCount() != 1) {
                    Library obj = (Library)c.newInstance();  
                    System.out.println(obj.toString());  
                    System.out.println(obj.someLibraryMethod());  
                    break;
                }
                Library obj = (Library)c.newInstance("titi");  
                System.out.println(obj.toString());  
                System.out.println(obj.someLibraryMethod()); 
            }  
            System.out.println(cl == cl3);
            System.out.println("===================");
            Method[] methodArray = cl3.getMethods();  
            for(Method m : methodArray){  
                System.out.println(m);  
            } 
            System.out.println("===================");
            
            methodArray = cl3.getDeclaredMethods();  
            for(Method m : methodArray){  
                System.out.println(m);  
            }  
            System.out.println("===================");
            Method methodMain = cl3.getMethod("main", String[].class);
            methodMain.invoke(null, (Object)new String[]{"1","2"});
            
            System.out.println("===================");
            ArrayList<String> strList = new ArrayList<>();
            strList.add("aaa");
            strList.add("bbb");

        //  strList.add(100);
            //获取ArrayList的Class对象,反向的调用add()方法,添加数据
            Class listClass = strList.getClass(); //得到 strList 对象的字节码 对象
            //获取add()方法
            Method m = listClass.getMethod("add", Object.class);
            //调用add()方法
            m.invoke(strList, 100);
            System.out.println(strList);
            System.out.println("===================");
            Field[] fields =  cl3.getDeclaredFields();
            for(Field f : fields){  
                System.out.println(f);  
            }  
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        InvocationHandler handler = new MyInvocationHandler(lib);
        LibraryInterface f = (LibraryInterface) Proxy.newProxyInstance(LibraryInterface.class.getClassLoader(),
                new Class[] { LibraryInterface.class },
                handler);
        System.out.println("===================");
        System.out.println(f.someLibraryMethod());
    }

}



调度方法调用的调用处理程序

package demoGradleOne;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler{

    private Library lib;
    
    public MyInvocationHandler(Library lib) {
        this.lib = lib;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = null;
        try {
            result = method.invoke(lib, args);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("use time = " + (end - start));
        return result;
    }

}

相关文章

  • 博客地址

    java注解-01、java注解-02、Git面试资源java反射-01、java反射-02、java反射-03为...

  • Java反射机制入门

    Java反射机制入门 一、什么是反射 JAVA反射机制(The JAVA reflection mechanism...

  • Java基础之反射

    Java基础之—反射(非常重要)Java中反射机制详解Java进阶之reflection(反射机制)——反射概念与...

  • 反射之一

    总结内容源自一下文章粗浅看java反射机制反射机制应用实践谈谈java反射机制Java Reflection(反射...

  • 反射之二

    总结内容源自一下文章粗浅看java反射机制反射机制应用实践谈谈java反射机制Java Reflection(反射...

  • Java基础之反射

    Java基础之反射 反射基本介绍 反射的使用通过反射调用属性和方法通过反射获取配置文件 反射基本介绍 Java反射...

  • Java 反射机制

    Java 反射机制 什么是反射 Java 反射是Java语言的一个很重要的特征,它使得Java具体了“动态性”。 ...

  • 一探究竟:Java反射效率低的原因到底在哪?

    预备知识 了解 Java 反射基本用法 看完本文可以达到什么程度 了解 Java 反射原理及 Java 反射效率低...

  • 面试官说:大家都说 Java 反射效率低,你知道原因在哪里么

    预备知识 了解 Java 反射基本用法 看完本文可以达到什么程度 了解 Java 反射原理及 Java 反射效率低...

  • Java面试题之JavaSE高级

    一、Java中的反射 1.说说你对Java中反射的理解 java中的反射首先是能够获取到Java...

网友评论

      本文标题:java 反射

      本文链接:https://www.haomeiwen.com/subject/tflykctx.html