java reflect 学习

1.如何实现从一个类名(”java.util.ArrayList”)到类(ArrayList.class)再到实例化(Object,相当于new ArrayList())的过程转化?这个过程中需要处理哪些异常?

public class Demo1{

       private int i = 1;

       public static void main(String[] args){

              try{

                     Class class1 = Class.forName(“reflect.Demo1”);

                     Demo1 demo1 = (Demo1)class1.newInstance();

                     System.out.println(demo1.i);

              }catch(ClassNotFoundException e) {

                    

              }catch(InstatiationException e1) {

                     e.printStackTrance();

              }catch(IllegaAccessException e2) {

                     e.printStackTrance();

              }

       }

}

 

2.请说明如何获得一个对象的类名、包名?

       public static void main(String[] args) {

              String packageName = Demo1.class.getPackage().getName();

              String objName = Demo1.class.getName();

              String objSimpleName = Demo1.class.getSimpleName();

       }

}

 

3.请说明如何获得一个类(Class)的所有域(Field),包括其父类的域。

import java.lang.reflect.Field;

public class Father {

       private String fname = "father";

       private int fage = 40;

       public static void main(String[] args) {

              try {

                     Class<Son> class1 = (Class<Son>) Class.forName("reflect.Son");

                     Son son = class1.newInstance();

                     Field[] fields = son.getClass().getDeclaredFields();   //getFields() 只能获得非private 以外的成员变量

                     for (Field field : fields) {

                            System.out.println(field.getName());

                     }

                     Field[] fields1 = son.getClass().getSuperclass().getDeclaredFields(); 

                     for (Field field : fields1) {

                            System.out.println(field.getName());

                     }

              } catch (ClassNotFoundException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (InstantiationException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (IllegalAccessException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

               

       }

       public String getFname() {

              return fname;

       }

       public void setFname(String fname) {

              this.fname = fname;

       }

       public int getFage() {

              return fage;

       }

       public void setFage(int fage) {

              this.fage = fage;

       }

}

class Son extends Father{

       private String sname = "son";

       private int sage = 20;

       public String getSname() {

              return sname;

       }

       public void setSname(String sname) {

              this.sname = sname;

       }

       public int getSage() {

              return sage;

       }

       public void setSage(int sage) {

              this.sage = sage;

       }

}

 

 

4.请说明如何获得一个类(Class)的方法,以及如何调用这些方法?

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Demo3 {

       public void show() {

              System.out.println("show method");

       }

       public void show(int i) {

              System.out.println("show method with param :" + i);

       }

       public int showReturn() {

              return 1;

       }

       public static void main(String[] args) {

              try {

                     Class class1 = Class.forName("reflect.Demo3");

                     Demo3 demo3 = (Demo3) class1.newInstance();

                     Method method1 = demo3.getClass().getDeclaredMethod("show");

                     Method method2 = demo3.getClass().getDeclaredMethod("show", int.class);

                     Method method3 = demo3.getClass().getDeclaredMethod("showReturn");

                     method1.invoke(demo3);

                     method2.invoke(demo3, 2);

                     Integer i = (Integer)method3.invoke(demo3);

                     System.out.println(i);

              } catch (ClassNotFoundException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (InstantiationException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (IllegalAccessException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (SecurityException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (NoSuchMethodException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (IllegalArgumentException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (InvocationTargetException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

       }

}