JAVA反射机制学习(二)

2014-11-23 21:35:47 · 作者: · 浏览: 40
kTrace(); } Person per1=null; Person per2=null; Person per3=null; Person per4=null; //取得全部的构造函数 Constructor cons[]=demo.getConstructors(); try{ per1=(Person)cons[0].newInstance(); per2=(Person)cons[1].newInstance("Rollen"); per3=(Person)cons[2].newInstance(20); per4=(Person)cons[3].newInstance("Rollen",20); }catch(Exception e){ e.printStackTrace(); } System.out.println(per1); System.out.println(per2); System.out.println(per3); System.out.println(per4); } }
输出:

[null 0]

[Rollen 0]

[null 20]

[Rollen 20]

5.返回一个类实现的接口:

package Reflect;
 
interface China{
    public static final String name="Rollen";
    public static  int age=20;
    public void sayChina();
    public void sayHello(String name, int age);
}
 
class Person implements China{
    public Person() {
         
    }
    public Person(String sex){
        this.sex=sex;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public void sayChina(){
        System.out.println("hello ,china");
    }
    @Override
    public void sayHello(String name, int age){
        System.out.println(name+"  "+age);
    }
    private String sex;
}
 
class hello{
    public static void main(String[] args) {
        Class
   demo=null;
        try{
            demo=Class.forName("Reflect.Person");
        }catch (Exception e) {
            e.printStackTrace();
        }
        //保存所有的接口
        Class
   intes[]=demo.getInterfaces();
        for (int i = 0; i < intes.length; i++) {
            System.out.println("实现的接口   "+intes[i].getName());
        }
    }
}


输出:实现的接口 Reflect.China
(注意,以下几个例子,都会用到这个例子的Person类,所以为节省篇幅,此处不再粘贴Person的代码部分,只粘贴主类hello的代码)

6.取得其他类中的父类:

class hello{
    public static void main(String[] args) {
        Class
   demo=null;
        try{
            demo=Class.forName("Reflect.Person");
        }catch (Exception e) {
            e.printStackTrace();
        }
        //取得父类
        Class
   temp=demo.getSuperclass();
        System.out.println("继承的父类为:   "+temp.getName());
    }
}
输出:继承的父类为: java.lang.Object;

7.获得其他类中的全部构造函数:

这个例子需要在程序开头添加import java.lang.reflect.*;

然后将主类编写为:

class hello{
    public static void main(String[] args) {
        Class
   demo=null;
        try{
            demo=Class.forName("Reflect.Person");
        }catch (Exception e) {
            e.printStackTrace();
        }
        Constructor
  cons[]=demo.getConstructors();
        for (int i = 0; i < cons.length; i++) {
            System.out.println("构造方法:  "+cons[i]);
        }
    }
}
输出:

构造方法: Reflect.Person()

构造方法: Reflect.Person(java.lang.String)

但是细心的读者会发现,上面的构造函数没有public 或者private这一类的修饰符

8.获取构造函数的修饰符:

class hello{
    public static void main(String[] args) {
        Class
   demo=null;
        try{
            demo=Class.forName("Reflect.Person");
        }catch (Exception e) {
            e.printStackTrace();
        }
        Constructor
  cons[]=demo.getConstructors();
        for (int i = 0; i < cons.length; i++) {
            Class
   p[]=cons[i].getParameterTypes();
            System.out.print("构造方法:  ");
            int mo=cons[i].getModifiers();
            System.out.print(Modifier.toString(mo)+" ");
            System.out.print(cons[i].getName());
            System.out.print("(");
            for(int j=0;j
  
   
输出:

构造方法: public Reflect.Person(){}

构造方法: public Reflect.Person(java.lang.String arg1){}

9.查看方法抛出的异常:

class hello{
    public static void main(String[] args) {
        Class
     demo=null;
        try{
            demo=Class.forName("Reflect.Person");
        }catch (Exception e) {
            e.printStackTrace();
        }
        Method method[]=demo.getMethods();
        for(int i=0;i
    
      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