zoukankan      html  css  js  c++  java
  • java反射基础方法

    java反射基础使用方法

    User.class
    public class User {
        private int age;
        private String name;
    
        public User() {
            super();
        }
        public User(String name) {
            super();
            this.name = name;
        }
        public User(int age, String name) {
            super();
            this.age = age;
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return "User [age=" + age + ", name=" + name + "]";
        }
    }
    
    反射的基础方法讲解
    public class TestReflect extends User implements Serializable { private static final long serialVersionUID = 6296302711043277522L; // 通过一个对象获得完整的包名和类名 // public static void main(String[] args) { // TestReflect testReflect = new TestReflect(); // System.out.println(testReflect.getClass().getName()); // } // 实例化Class类对象 // public static void main(String[] args) throws ClassNotFoundException { // Class<?> class1 = null; // Class<?> class2 = null; // Class<?> class3 = null; // // class1 = Class.forName("com.example.demo.reflect.TestReflect"); // class2 = new TestReflect().getClass(); // class3 = TestReflect.class; // // System.out.println("类名称 " + class1.getName()); // System.out.println("类名称 " + class2.getName()); // System.out.println("类名称 " + class3.getName()); // // // } // 获取一个对象的父类与实现的接口 // public static void main(String[] args) throws ClassNotFoundException { // Class<?> clazz = Class.forName("com.example.demo.reflect.TestReflect"); // //取得父类 // Class<?> parentClass = clazz.getSuperclass(); // System.out.println("clazz的父类为:" + parentClass.getName()); // // // // clazz的父类为: java.lang.Object // // 获取所有的接口 // Class<?> intes[] = clazz.getInterfaces(); // System.out.println("clazz实现的接口有:"); // for (int i = 0; i < intes.length; i++) { // System.out.println((i + 1) + ":" + intes[i].getName()); // } // // clazz实现的接口有: // // 1:java.io.Serializable // // } // 通过反射机制实例化一个类的对象 // public static void main(String[] args) throws Exception { // Class<?> class1 = null; // class1 = Class.forName("com.example.demo.entity.User"); // // 第一种方法,实例化默认构造方法,调用set赋值 // User user = (User) class1.newInstance(); // user.setAge(20); // user.setName("hly"); // System.out.println(user); // // 结果 User [age=20, name=Rollen] // // 第二种方法 取得全部的构造函数 使用构造函数赋值 // Constructor<?> cons[] = class1.getConstructors(); // // 查看每个构造方法需要的参数 // for(int i=0;i<cons.length;i++){ // Class<?> clazzs[] = cons[i].getParameterTypes(); // System.out.print("cons["+i+"] ("); // for (int j = 0; j < clazzs.length; j++) { // if (j == clazzs.length - 1) // System.out.print(clazzs[j].getName()); // else // System.out.print(clazzs[j].getName() + ","); // } // System.out.println(")"); // } // user = (User) cons[1].newInstance("Rollen"); // System.out.println(user); // // 结果 User [age=0, name=Rollen] // user = (User) cons[0].newInstance(20, "Rollen"); // System.out.println(user); // // 结果 User [age=20, name=Rollen] // } // 获取某个类的全部属性 // public static void main(String[] args) throws ClassNotFoundException { // Class<?> clazz = Class.forName("com.example.demo.reflect.TestReflect"); // System.out.println("===============本类属性==============="); // // 取得本类的全部属性 // Field[] field = clazz.getDeclaredFields(); // for (int i = 0; i < field.length; i++) { // // 权限修饰符 // int mo = field[i].getModifiers(); // System.out.println("mo:"+mo); // String priv = Modifier.toString(mo); // // 属性类型 // Class<?> type = field[i].getType(); // System.out.println(priv + "1 " + type.getName() + "1 " + field[i].getName() + ";"); // } // // System.out.println("==========实现的接口或者父类的属性=========="); // // 取得实现的接口或者父类的属性 // Field[] filed1 = clazz.getFields(); // for (int j = 0; j < filed1.length; j++) { // // 权限修饰符 // int mo = filed1[j].getModifiers(); // String priv = Modifier.toString(mo); // // 属性类型 // Class<?> type = filed1[j].getType(); // System.out.println(priv + " " + type.getName() + " " + filed1[j].getName() + ";"); // } // // } // 获取某个类的全部方法 // // public static void main(String[] args) throws ClassNotFoundException { // Class<?> clazz = Class.forName("com.example.demo.entity.User"); // Method method[] = clazz.getMethods(); // for (int i = 0; i < method.length; ++i) { // Class<?> returnType = method[i].getReturnType(); // Class<?> para[] = method[i].getParameterTypes(); // int temp = method[i].getModifiers(); // System.out.print(Modifier.toString(temp) + " "); // System.out.print(returnType.getName() + " "); // System.out.print(method[i].getName() + " "); // System.out.print("("); // for (int j = 0; j < para.length; ++j) { // System.out.print(para[j].getName() + " " + "arg" + j); // if (j < para.length - 1) { // System.out.print(","); // } // } // Class<?> exce[] = method[i].getExceptionTypes(); // if (exce.length > 0) { // System.out.print(") throws "); // for (int k = 0; k < exce.length; ++k) { // System.out.print(exce[k].getName() + " "); // if (k < exce.length - 1) { // System.out.print(","); // } // } // } else { // System.out.print(")"); // } // System.out.println(); // } // } // 通过反射机制调用某个类的方法 // public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { // Class<?> clazz = Class.forName("com.example.demo.reflect.TestReflect"); // // 调用TestReflect类中的reflect1方法 // Method method = clazz.getMethod("reflect1"); // method.invoke(clazz.newInstance()); // // 调用TestReflect的reflect2方法 // method = clazz.getMethod("reflect2",int.class,String.class); // method.invoke(clazz.newInstance(),20,"hly"); // // // } // // public void reflect1() { // System.out.println("Java 反射机制 - 调用某个类的方法1."); // } // public void reflect2(int age, String name) { // System.out.println("Java 反射机制 - 调用某个类的方法2."); // System.out.println("age -> " + age + ". name -> " + name); // } // // // private String proprety = null; // // public static void main(String[] args) throws Exception { // Class<?> clazz = Class.forName("com.example.demo.reflect.TestReflect"); // Object obj = clazz.newInstance(); // Field field = clazz.getDeclaredField("proprety"); // field.setAccessible(true); // field.set(obj,"javafanshe"); // System.out.println(field.get(obj)); // System.out.println(obj); // // 获取类加载器的方法 // TestReflect testReflect = new TestReflect(); // System.out.println("类加载器 " + testReflect.getClass().getClassLoader().getClass().getName()); // // } // public static void main(String[] args) throws Exception { // MyInvocationHandler demo = new MyInvocationHandler(); // Object object = demo.bind(new RealSubject()); // System.out.println("object:"+object); // Subject sub = (Subject) object; // String info = sub.say("Rollen", 20); // System.out.println(info); // } // //} //interface Subject { // public String say(String name,int age); //} // //class RealSubject implements Subject { // public String say(String name,int age){ // return name + " " + age; // } //} // //class MyInvocationHandler implements InvocationHandler { // private Object obj = null; // public Object bind(Object obj) { // this.obj = obj; // /*public static Object newProxyInstance(ClassLoader loader, // Class<?>[] interfaces, // InvocationHandler h) // throws IllegalArgumentException*/ // return Proxy.newProxyInstance( // obj.getClass().getClassLoader(), //用哪个类的加载器去加载代理对象 // obj.getClass().getInterfaces() /*interfaces的动态代理类需要实现的接口*/,this);//h:动态代理方法在执行时,会调用h里面的invoke方法去执行 // } // // public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // System.out.println(this); // System.out.println("!!!!!"+this.obj); // Object temp = method.invoke(this.obj, args); // return temp; // } public static void main(String[] args) throws Exception { int[] temp = { 1, 2, 3, 4, 5 }; Class<?> demo = temp.getClass().getComponentType(); System.out.println("数组类型: " + demo.getName()); System.out.println("数组长度 " + Array.getLength(temp)); System.out.println("数组的第一个元素: " + Array.get(temp, 0)); Array.set(temp, 0, 100); System.out.println(Array.class.getName()); System.out.println("修改之后数组第一个元素为: " + Array.get(temp, 0)); } } /** * 在java中有三种类类加载器。 * * 1)Bootstrap ClassLoader 此加载器采用c++编写,一般开发中很少见。 * * 2)Extension ClassLoader 用来进行扩展类的加载,一般对应的是jrelibext目录中的类 * * 3)AppClassLoader 加载classpath指定的类,是最常用的加载器。同时也是java中默认的加载器。 * * 如果想要完成动态代理,首先需要定义一个InvocationHandler接口的子类,已完成代理的具体操作。 * * @author xsoftlab.net * */
  • 相关阅读:
    20155327 嵌入式C语言课堂补交
    2017-2018-1 20155327 《信息安全系统设计基础》课程总结
    2017-2018-1 20155327 《信息安全系统设计基础》第十四周学习总结
    2017-2018-1 20155327 实验五 通讯协议设计
    2017-2018-1 20155327 《信息安全系统设计基础》第十三周学习总结
    《Java程序设计》课堂实践内容总结
    20155337 2016-2017-2《Java程序设计》课程总结
    20155337 《网络安全编程》实验五实验报告
    # 20155337 《Android程序设计》实验四实验报告
    20155337 《Java程序设计》实验三(敏捷开发与XP实践)实验报告
  • 原文地址:https://www.cnblogs.com/h-java/p/10026760.html
Copyright © 2011-2022 走看看