Java反射初始化类调用类中的方法

1.通过无参构造方法实例化对象

无参构造方法,类

public class ReflectServiceImpl {

  public ReflectServiceImpl() {
  }

  public void sayHello(String name) {
    System.out.println("hello" + name);
  }
}

反射调用方式

public static void main(String[] args) {
    ReflectServiceImpl reflectServiceImpl = null;
    try {
      reflectServiceImpl = (ReflectServiceImpl) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl").newInstance();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    reflectServiceImpl.sayHello("张三");
  }

2. 有参构造函数,对象实例化

有参构造函数类

public class ReflectServiceImpl2 {

 private int age;
  private String name;

  public ReflectServiceImpl2(String name, Integer age) {
    this.name = name;
    this.age = age;
  }

  public void sayHello() {
    System.out.println("hello" + name + ",年龄" + age);
  }
}

反射调用方式

public static void main(String[] args) {
    ReflectServiceImpl2 reflectServiceImpl = null;
    try {
      reflectServiceImpl = (ReflectServiceImpl2) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
          .getConstructor(String.class, Integer.class)
          .newInstance("张三", 13);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException 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();
    } catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    reflectServiceImpl.sayHello();
  }

主要通过getConstructor方法指定构造方法参数的类型和个数。注意通过反射调用构造时,构造函数中的参数必须是类,所以参数不能是基本类型

3. 通过方法名称,调用类中的方法

public class ReflectServiceImpl2 {

  private int age;
  private String name;

  public ReflectServiceImpl2(String name, Integer age) {
    this.name = name;
    this.age = age;
  }

  public void sayHello() {
    System.out.println("hello" + name + ",年龄" + age);
  }

  public void sayHello(String name, Integer age) {
    System.out.println("hello" + name + ",年龄" + age);
  }
}

调用无参的sayHello

public static void main(String[] args) {
    ReflectServiceImpl2 reflectServiceImpl = null;
    try {
      reflectServiceImpl = (ReflectServiceImpl2) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
          .getConstructor(String.class, Integer.class).newInstance("张三", 13);
      Method method = reflectServiceImpl.getClass().getMethod("sayHello", null);
      method.invoke(reflectServiceImpl, null);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException 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();
    } catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

getMethod 和调用方法invoke参数都传入null。
调用两个参数的sayHello

public static void main(String[] args) {
    ReflectServiceImpl2 reflectServiceImpl = null;
    try {
      reflectServiceImpl = (ReflectServiceImpl2) Class.forName("com.lean.ssm.chapter2.reflect.ReflectServiceImpl2")
          .getConstructor(String.class, Integer.class).newInstance("张三", 13);
      Method method = reflectServiceImpl.getClass().getMethod("sayHello", String.class, Integer.class);
      method.invoke(reflectServiceImpl, "李四", 16);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IllegalAccessException 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();
    } catch (NoSuchMethodException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SecurityException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

getMethod 和调用方法invoke参数都传入参数对应的类。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值