java反射调用方法

来源:互联网 发布:淘宝合约机是什么意思 编辑:程序博客网 时间:2024/05/16 10:03

package src.getClass;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class GetMethod {
 
 public GetMethod()
 {
  System.out.println("yusdfsdkfljsflksj");
 }

 public void doit()
 {
  
 }
 
 public void getid()
 {
  System.out.println("没有的参数的方法的调用!");
 }
 
 public String some(String s)
 {
  System.out.println(s);
  return "yuxk";
 }
 public void getMethod()throws Exception
 {
  Class clazz = Class.forName("src.getClass.GetMethod");
  
  Constructor construtor = clazz.getConstructor(null);
 
  Object getMethod = construtor.newInstance(null);
  Class methodParam[] = new Class[1];
  methodParam[0] = String.class;
  Method method = clazz.getMethod("some", methodParam);
  Object object[] = new Object[1];
  object[0] = "wo kao shen qi a ";
  Object returno =  method.invoke(getMethod, object);
  System.out.println((String)returno);
 }
 
 static public void main(String args[]) throws Exception
 {
  new GetMethod().getMethod();
 }
}
 

原创粉丝点击