invoke

1717 단어 자바
/**
 * 
 */
package test8_15;

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

/**   
 *  
 * Simple to Introduction  
 * @ProjectName:  [  ] 
 * @Package:      [test8_15.testInvoke.java]  
 * @ClassName:    [testInvoke]   
 * @Description:  [      ]   
 * @Author:       [   ]   
 * @CreateDate:   [2014-8-15   3:43:18]   
 * @UpdateUser:   [mengxianguang]   
 * @UpdateDate:   [2014-8-15   3:43:18]   
 * @UpdateRemark: [        ]  
 * @Version:      [v1.0] 
 *    
 */
public class testInvoke {
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, SecurityException, NoSuchMethodException {
		Class clazz;//     
		Object obj=null;//      
		try {
			 clazz = Class.forName("java.util.HashSet");//         
			 obj=clazz.newInstance();//       
			 Method[]methods=clazz.getMethods();//        
			for(Method method:methods){
				System.out.println(method.getName());//        
			}
			//              add(            )
			//        
			Method method=clazz.getMethod("add", Object.class);
			//     ,     method
			method.invoke(obj, "a");
			method.invoke(obj, "b");
			method.invoke(obj, "c");
			//           
			System.out.println(obj);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	}
}

좋은 웹페이지 즐겨찾기