method.invoke,methodinvoke方法

http://www.itjxue.com  2023-01-08 22:10  来源:未知  点击次数: 

Method对象中invoke方法中第一个参数表示____

invoke方法的第一个参数是一个对象。

此对象可以为:①方法持有者;②方法持有者的继承者。

例子:父类有方法

public?class?Father?{

????public?void?say(String?name){

????????System.out.println("我是father叫"+name);

????}

}

子类有方法

public?class?Son?extends?Father?{

????@Override

????public?void?say(String?name){

????????System.out.println("我是son我叫"+name);

????}

}

测试类

public?class?TestInvoke?{

????public?void?test()?throws?Exception?{

????????Class?clazz?=?Class.forName("CloneObject.Father");//反射父类

????????Father?father?=?new?Father();//实例化父类

????????Son?son?=?new?Son();//实例化子类

????????Object?object=clazz.newInstance();//获得一个新的父类对象

????????

????????Method?method?=?clazz.getMethod("say",String.class);

????????method.invoke(father,"爸爸");

????????method.invoke(son,"孩子");

????????method.invoke(object,"新的父亲");

????}

????public?static?void?main(String[]?args)?throws?Exception?{

????????new?TestInvoke().test();

????}

}

结果

我是father叫爸爸

我是son我叫孩子

我是father叫新的父亲

java Method invoke 抛出异常

InvocationTargetException异常由Method.invoke(obj, args...)方法抛出。当被调用的方法的内部抛出了异常而没有被捕获时,将由此异常接收。

示例:

package com.zzj.test.reflect;

public class Reflect {

public void run(int i) throws ZeroException {

B b = new B();

b.run(i);

}

}

class B {

public void run(int i) throws ZeroException {

if (i 0) {

throw new ZeroException("参数不能小于零!");

}

System.out.println("参数:" + i);

}

}

class ZeroException extends Exception {

private static final long serialVersionUID = 1L;

private String detailMessage;

public ZeroException(String detailMessage) {

this.detailMessage = detailMessage;

}

public String getMessage() {

return detailMessage;

}

}

测试:

package com.zzj.test.reflect;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Test {

public static void main(String[] args) {

try {

Class? clazz = Class.forName("com.zzj.test.reflect.Reflect");

Method method = clazz.getMethod("run", int.class);

method.invoke(clazz.newInstance(), -1);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

System.out.println("此处接收被调用方法内部未被捕获的异常");

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

}

}

}

输出:

此处接收被调用方法内部未被捕获的异常

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at com.zzj.test.reflect.Test.main(Test.java:11)

Caused by: com.zzj.test.reflect.ZeroException: 参数不能小于零!

at com.zzj.test.reflect.B.run(Reflect.java:13)

at com.zzj.test.reflect.Reflect.run(Reflect.java:6)

... 5 more

也可以直接打印目标异常:

package com.zzj.test.reflect;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class Test {

public static void main(String[] args) {

try {

Class? clazz = Class.forName("com.zzj.test.reflect.Reflect");

Method method = clazz.getMethod("run", int.class);

method.invoke(clazz.newInstance(), -1);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

System.out.println("此处接收被调用方法内部未被捕获的异常");

Throwable t = e.getTargetException();// 获取目标异常

t.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

}

}

}

输出:

此处接收被调用方法内部未被捕获的异常

com.zzj.test.reflect.ZeroException: 参数不能小于零!

at com.zzj.test.reflect.B.run(Reflect.java:13)

at com.zzj.test.reflect.Reflect.run(Reflect.java:6)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at com.zzj.test.reflect.Test.main(Test.java:11)

method的invoke方法怎么用

public?class?Names?{

?public?String?firstName?=?"Callan";

?public?String?lastName?=?"Wang";

?

?public?String?getFullName(){

??return?firstName?+?"?"?+?lastName;

?}

?

?public?String?getFullName(String?firstName,String?lastName){

??return?firstName?+?"?"?+?lastName;

?}

}

?

?

?

import?java.lang.reflect.InvocationTargetException;

import?java.lang.reflect.Method;

public?class?Test?{

?

?public?static?void?main(String[]?args)?throws?SecurityException,?NoSuchMethodException,?IllegalArgumentException,?IllegalAccessException,?InvocationTargetException?{

??

??Names?methods?=?new?Names();

?

??//?调用getFullName()方法

??Method?method1?=?methods.getClass().getMethod("getFullName");

??Object?obj1?=?method1.invoke(methods,?null);

??System.out.println(obj1);

??

??//?调用getFullName(String?firstName,String?lastName)方法

??Method?method2?=?methods.getClass().getMethod("getFullName",?new?Class[]{String.class,String.class});//方法带有两个String类型的参数

??//?invoke(从中调用基础方法的对象,用于方法调用的参数)

??Object?obj2?=?method2.invoke(methods,?new?String[]{"Callan","Wang"});

??System.out.println(obj2);

?}

}

如何把method.invoke后的返回值转成数组或集合类型

Java code

Object array = method.invoke(obj, new Object[]{});

int length = java.lang.reflect.Array.getLength(array);

ListObject list = new ArrayListObject();

for (int i=0; ilength; i++) {

list.add(java.lang.reflect.Array.get(array, i));

}

就是这样

java反射中method类中的invoke方法是做什么的,他有什么作用?

首先Method类代表一个方法,所以invoke(调用)就是调用Method类代表的方法。它可以让你实现动态调用,例如你可以动态的传人参数。下面是一个简单的例子。

public?class?MethodTest

{

????public?static?void?main(String[]?args)

????{

????????String?[]?names?={"tom","tim","allen","alice"};

????????Class??clazz?=?Test.class;

????????try

????????{

????????????Method?method?=?clazz.getMethod("sayHi",?String.class);

????????????for(String?name:names)

????????????????method.invoke(clazz.newInstance(),name);

????????}?catch?(NoSuchMethodException?e)

????????{

????????????e.printStackTrace();

????????}?catch?(IllegalAccessException?e)

????????{

????????????e.printStackTrace();

????????}?catch?(IllegalArgumentException?e)

????????{

????????????e.printStackTrace();

????????}?catch?(InvocationTargetException?e)

????????{

????????????e.printStackTrace();

????????}?catch?(InstantiationException?e)

????????{

????????????e.printStackTrace();

????????}

????}

}

class?Test

{

????public?void?sayHi(String?name)

????{

????????System.out.println("Hi?"+name);

????}

}

(责任编辑:IT教学网)

更多

推荐人物新闻文章