1. 程式人生 > >java反射:加載成員方法

java反射:加載成員方法

pri ssi ID otf cati sys leg tar security

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方法!!!");
	}
}

  

java反射:加載成員方法