invoke = method.invoke(set,args);
时间: 2024-05-27 14:14:41 浏览: 11
This line of code is using Java's Reflection API to dynamically invoke a method on an object instance.
The "method" variable refers to the method that we want to invoke, and the "set" variable refers to the object instance on which we want to invoke the method.
The "args" variable is an array of arguments that we want to pass to the method when it is invoked.
The "invoke" method is called on the "method" variable, with the "set" and "args" variables passed as arguments. This causes the method to be invoked on the "set" object instance with the given arguments.
The result of the method invocation is returned and assigned to the "invoke" variable.
相关问题
method.invoke(target, args);
This line of code uses reflection in Java to invoke a method on a target object with a set of arguments. The "method" variable represents the method being invoked, while the "target" variable represents the object on which the method is being invoked. The "args" variable represents the arguments being passed to the method.
In other words, this line of code dynamically calls a method on an object, even if the method is not known at compile time. This is useful for scenarios where the code needs to be flexible and adaptable to different situations.
package step4; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Reflect_stu { public static void main(String[] args) throws InvocationTargetException { //使用反射调用 Class clazz = null; try { clazz = Class.forName("step4.Apple"); /********** Begin *********/ Method setPriceMethod = clazz.getMethod("setPrice", double.class); Constructor appleConstructor = clazz.getConstructor(); Object apple = appleConstructor.newInstance(); setPriceMethod.invoke(apple, 14); Method getPriceMethod = clazz.getMethod("getPrice"); System.out.println(getPriceMethod.invoke(apple)); Method getTotal = clazz.getMethod("getTotal", double.class, int.class); System.out.println(getTotal.invoke(apple, 20, 24)); /********** End *********/ } catch (Exception e) { e.printStackTrace(); } } } class Apple { private double price; private int count; public Apple() { } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public double getTotal(double price, int count) { return price * count; } }
这段代码是一个使用反射调用的示例,它包括一个主类 Reflect_stu 和一个 Apple 类。在主类中,首先使用 Class.forName 方法获取 Apple 类的 Class 对象,然后使用 getMethod 方法获取 Apple 类中的 setPrice、getPrice 和 getTotal 方法,并使用 getConstructor 方法获取 Apple 类的默认构造方法的 Constructor 对象。接着使用 newInstance 方法创建一个 Apple 对象,并使用 invoke 方法调用 setPrice 方法设置 Apple 对象的价格为 14,使用 invoke 方法调用 getPrice 方法获取 Apple 对象的价格并输出,最后使用 invoke 方法调用 getTotal 方法计算 Apple 对象的总价并输出。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)