Application of Java dynamic call class method

xiaoxiao2021-03-06  41

Good articles that are seen today and share them:

http://www.internetism.org/qbbs/showannounce.asp?boardid=1&rootid=375&ID=375

http://www.internetism.org/qbbs/showannounce.asp?boardidid=1&rootid=375&ID=376

Here is the code I have modified:

Package testclass;

PUBLIC CLASS1 {public class1 () {}

Public Int Add (int A, int b) {RETURN A B;}

}

Package DYN_CALLMETHOD;

Import java.lang.reflect. *;

Import TestClass.class1;

Public class calclass {

Public calclass () {

}

Public static void main (string args []) {

Try {

Class CLS = Class.Forname ("TestClass.class1");

System.out.println ("Load The Target Class");

Class Partypes [] = new class [2];

Partypes [0] = integer.type;

Partypes [1] = integer.Type;

Method method ("add", partypes);

System.out.Println ("Get The Method of The Class");

//testclass.class1 method1 (); /// class const = class.Forname ("testclass.class1"); class par [] = null; constructor ct = cls.getconstructor (PAR); // Since the constructor of Class1 is no argument, I will give getConstructor.

Entered a parameter / in the NULL of the Class array

Object arglist [] = new object [2];

Arglist [0] = new integer (37);

Arglist [1] = new integer (47);

// Object retobj = method.invoke (methobj, arglist); Object retobj = method.invoke (ct.newinstance (null), arglist;

Integer Retval = (Integer) Retobj;

System.out.println (RetVal.intValue ());

}

Catch (Exception E) {

System.err.Println (e);

}

}

}

THank you for everyones suggestions!

转载请注明原文地址:https://www.9cbs.com/read-68168.html

New Post(0)