Java反射

Java反射

 

 

package crazypebble.reflectiontest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

publicclass ReflectionTest extends Objectimplements ActionListener,Serializable{

// 成员变量
privateint bInt;
public Integer bInteger =new Integer(4);
public String strB ="crazypebble";
private String strA;

// 构造函数
public ReflectionTest() {
    }

protected ReflectionTest(int id, String name) {
    }

// 成员方法
publicint abc(int id, String name) {
        System.out.println("crazypebble ---> "+ id+"-"+ name);
return0;
    }

protectedstaticvoid edf() {
    }

    @Override
publicvoid actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
    }
}

package crazypebble.reflectiontest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;


public class ReflectionTest extends Object implements ActionListener,Serializable{

// 成员变量
    private int bInt;
public Integer bInteger = new Integer(4);
public String strB = "crazypebble";
private String strA;

// 构造函数
    public ReflectionTest() {

    }

protected ReflectionTest(int id, String name) { 

    }

// 成员方法
    public int abc(int id, String name) {
        System.out.println("crazypebble ---> " + id + "-" + name);
return 0;
    }

protected static void edf() {

    }

    @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
        
    }

}
 
 
 
 

1、获取构造方法

  Class类提供了四个public方法,用于获取某个类的构造方法。

    Constructor getConstructor(Class[] params) 根据构造函数的参数,返回一个具体的具有public属性的构造函数

    Constructor getConstructors() 返回所有具有public属性的构造函数数组

    Constructor getDeclaredConstructor(Class[] params) 根据构造函数的参数,返回一个具体的构造函数(不分public和非public属性)

    Constructor getDeclaredConstructors() 返回该类中所有的构造函数数组(不分public和非public属性)

  由于Java语言是一种面向对象的语言,具有多态的性质,那么我们可以通过构造方法的参数列表的不同,来调用不同的构造方法去创建类的实例。同样,获取不同的构造方法的信息,也需要提供与之对应的参数类型信息;因此,就产生了以上四种不同的获取构造方法的方式。

 
 
 
在获取类的成员方法时,有一个地方值得大家注意,就是getMethods()方法和getDeclaredMethods()方法。

    getMethods():用于获取类的所有的public修饰域的成员方法,包括从父类继承的public方法和实现接口的public方法;

    getDeclaredMethods():用于获取在当前类中定义的所有的成员方法和实现的接口方法,不包括从父类继承的方法。

  大家可以查考一下开发文档的解释:

getMethods() - Returns an array containing Method objects for all public methods for the class C represented by this Class.

         Methods may be declared in C, the interfaces it implements or in the superclasses of C.

         The elements in the returned array are in no particular order.

getDeclaredMethods() - Returns a Method object which represents the method matching the specified name and parameter types

             that is declared by the class represented by this Class.

  因此在示例代码的方法get_Reflection_Method(...)中,ReflectionTest类继承了Object类,实现了actionPerformed方法,并定义如下成员方法:

    

  通过这两个语句执行后的结果不同:

  a、Method[] methods = temp.getDeclaredMethods()执行后结果如下:

    

  b、Method[] methods = temp.getMethods()执行后,结果如下:

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值