Java methods for invoking different parameters through reflection

Source: Internet
Author: User
Import Java.lang.reflect.Method;
			public class Testreflect {/** * @param args */public static void main (string[] args) {try {Wu w=new Wu ();
			Object[] Argspara=new object[]{};
			Testreflect.invokemethod (W, "Outinfo", Argspara);
			Argspara=new object[]{"WU"};
			Testreflect.invokemethod (W, "Outinfo", Argspara);
			Argspara=new object[]{"WU", 2};
		Testreflect.invokemethod (W, "Outinfo", Argspara);
		catch (Exception e) {e.printstacktrace (); /** * Reflection Invocation Method * @param newObj an object * @param the method name of the MethodName object * @param args parameter array * @return return value * @t 
		Hrows Exception */public static object InvokeMethod (Object newObj, String methodname, object[] args) throws Exception {
		Class Ownerclass = Newobj.getclass ();
		class[] Argsclass = new Class[args.length];
		for (int i = 0, j = args.length I < J; i++) {Argsclass[i] = Args[i].getclass ();
		Method method = Ownerclass.getmethod (methodname, Argsclass);
	Return Method.invoke (NEWOBJ, args); }
}

Class wu{public void Outinfo () {System.out.println ("Wu.outinfo"); 
    public void Outinfo (string s) {System.out.println ("Wu.outinfo (string s):" +s); 
    public void Outinfo (string S,integer N) {System.out.println ("Wu.outinfo (String s,int N):" +s+n); } 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.