- package org.mytest;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- /*
- * 动态类加载及反射演示类
- */
- public class DynamicClassLoader {
- private String attribute = "我是DynamicClassLoader类的属性!";
- public String str2 = "hi";
- public String getAttribute() {
- return attribute;
- }
- public DynamicClassLoader() {
- }
- public void setAttribute(String attribute) {
- this.attribute = attribute;
- }
- public int getAttribute2() {
- return 110;
- }
- protected void protectedMethod() {
- }
- private void privateMethod() {
- }
- public static void main(String s[]) throws ClassNotFoundException,
- InstantiationException, IllegalAccessException,
- IllegalArgumentException, InvocationTargetException {
- // //动态加载//
- DynamicClassLoader dcl0 = new DynamicClassLoader();// 这是我们通常创建一个类实例的做法--new
- System.out.println("new类实例,并打印实例dcl0的属性 = " + dcl0.getAttribute());
- Class class1 = Class.forName("org.mytest.DynamicClassLoader");
- DynamicClassLoader dcl1 = (DynamicClassLoader) class1.newInstance();
- System.out.println("Class.forName动态加载获得的类实例,并打印实例dcl1的属性 = " + dcl1.getAttribute());
- Class class2 = ClassLoader.getSystemClassLoader().loadClass("org.mytest.DynamicClassLoader");
- DynamicClassLoader dcl2 = (DynamicClassLoader) class2.newInstance();
- System.out.println("ClassLoader.loadClass动态加载获得的类实例,并打印实例dcl2的属性 = " + dcl2.getAttribute());
- System.out.println("dcl1==dcl2:" + (dcl1==dcl2));
- System.out.println("dcl1==dcl0:" + (dcl1==dcl0));
- System.out.println("dcl2==dcl0:" + (dcl1==dcl0));
- System.out.println("dcl0.getClass().getClassLoader():" + (dcl0.getClass().getClassLoader()));
- System.out.println("dcl1.getClass().getClassLoader():" + (dcl1.getClass().getClassLoader()));
- System.out.println("dcl2.getClass().getClassLoader():" + (dcl2.getClass().getClassLoader()));
- //讲解如果要求按照业务逻辑定义自己的对象比对策略(如hibernate的po可以仅仅比对
- //主键、主键等则两个po等),则重写(覆盖)equals方法和hashcode方法
- // ///方法反射//
- DynamicClassLoader d = new DynamicClassLoader();
- Object o = (Object) d;
- System.out.println("o.getClass() = " + o.getClass());
- Method[] methods = o.getClass().getMethods();
- System.out.println("methods.length = " + methods.length);
- for (int i = 0; i < methods.length; i++) {
- System.out.print("method" + (i + 1) + ":/t");
- System.out.println(methods[i]);
- }
- System.out.println("---------------------");
- Method[] methods2 = o.getClass().getDeclaredMethods();
- System.out.println("methods2.length = " + methods2.length);
- for (int i = 0; i < methods2.length; i++) {
- System.out.print("method" + (i + 1) + ":/t");
- System.out.println(methods2[i]);
- }
- /穿插属性反射//
- System.out.println("--------穿插属性反射--------");
- Field[] fields = o.getClass().getDeclaredFields();
- System.out.println(fields.length);
- for(int i = 0; i<fields.length; i++){
- System.out.print("field" + (i + 1) + ":/t");
- System.out.println(fields[i]);
- }
- System.out.println("---------继续方法反射---------");
- /继续方法反射//
- for(int i = 0; i<methods.length; i++){
- System.out.print("method" + (i + 1) + ":/t");
- System.out.println(methods[i]);
- Method method = methods[i];
- String methodName = method.getName();
- String returnType = method.getReturnType().getName();
- if(methodName.startsWith("get")&&!methodName.startsWith("getClass")){
- if(returnType.equals("java.lang.String")){
- String returnval = (String)method.invoke(d,null);
- System.out.println("java.lang.String reval = "+returnval);
- }else if(returnType.equals("String")){
- String returnval = (String)method.invoke(d,null);
- System.out.println("String reval = "+returnval);
- }else if(returnType.equals("java.lang.Integer")){
- Integer returnval = (Integer)method.invoke(d,null);
- System.out.println("Integer reval = "+returnval);
- }else if(returnType.equals("int")){
- Integer returnval = (Integer)method.invoke(d,null);
- System.out.println("int reval = "+returnval);
- }
- }
- }
- }
- }
- /*
- * 几点结论:
- * 对于private的方法getMethods()不反射、getDeclaredMethods()反射;
- * 对于protected的方法getMethods()不反射、getDeclaredMethods()反射;
- * getMethods()反射继承的和自身的public方法、getDeclaredMethods()反射自身声明的所有方法。
- */
动态类加载及反射演示类
最新推荐文章于 2024-03-10 16:41:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)