java反射:加载成员方法

java反射:加载成员方法

package com.ma.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;

import com.ma.bean.StudentBean;

/**
 * 反射:加载成员方法
 * @author ma
 *
 */
public class Demo3 {
	
	/**
	 * 测试无参成员 方法
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test1() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		//加载类
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		
		//加载方法
		Method method = clazz.getMethod("eat",null);
		
		//调用方法
		method.invoke(new StudentBean(), null);
		
	}
	/**
	 * 测试有参成员 方法
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test2() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		//加载类
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		
		//加载方法
		Method method = clazz.getMethod("play", String.class,int.class);
		
		//调用方法
		method.invoke(new StudentBean(), "张三",45);
		
		
	}
	/**
	 * 测试可变参成员 方法
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test3() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		//加载类
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		
		//加载方法
		Method method = clazz.getMethod("run", int[].class);
		
		//调用方法
		method.invoke(new StudentBean(), new int[]{1,2,3});
	}
	/**
	 * 测试私有成员 方法
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test4() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		//加载类
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		
		//加载方法
		Method method = clazz.getDeclaredMethod("jump", null);
		
		//设置私有方法访问权限 
		method.setAccessible(true);
		
		//调用方法
		method.invoke(new StudentBean(), null);
	}
	/**
	 * 测试静态成员方法
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test5() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		//加载类
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		
		//加载方法
		Method method = clazz.getMethod("sleep", int.class);
		
		//调用方法,静态方法不要对象就可以调用方法
		method.invoke(null, 34);
	}
	/**
	 * main方法的测试
	 * @throws ClassNotFoundException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test6() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		//加载类
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		
		//加载方法
		Method method = clazz.getMethod("main", String[].class);
		
		//调用main方法,静态方法不要对象就可以调用方法,由于main方法要折封数组,所以有两种方法调用main方法
		//第一种调用
		//method.invoke(null, new Object[]{new String[]{"aa","bb"}});
		//第二种调用
		method.invoke(null, (Object)new String[]{"aa","cc"});
	}
	
}

  StudentBean类

package com.ma.bean;
/**
 * 学生类
 * @author ma
 *
 */
public class StudentBean {
	
	public String name = "刘备";
	public int age;
	private String work = "教师";
	private static String sex = "男";
	
	
	public StudentBean() {
		super();
	}

	public void eat(){
		System.out.println("吃饭了。。。。。。。。。。。。。。");
	}
	
	public void play(String name,int age){
		System.out.println(name+":去玩了哟。。。。。。。。。"+age);
	}
	
	public void run(int  ...intaa){
		System.out.println(":这是可变参数方法");
	}
	
	private void jump(){
		System.out.println("这是私有方法");
	}
	
	public static void sleep(int age){
		System.out.println(age+":这是静态方法");
	}
	
	public static void main(String[] args) {
		System.out.println("这是main方法!!!");
	}
}

  

转载于:https://www.cnblogs.com/majingang/p/9116059.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值