写一个工具类Utils, 1.能实现调用对象中指定方法名字和参数列表方法的调用 2.实现给指定对象任意属性的赋值操作用java代码
时间: 2023-09-16 20:05:50 浏览: 158
java utils 工具类
### 回答1:
public class Utils {
public static void invokeMethod(Object object, String methodName, Object[] params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = object.getClass().getDeclaredMethod(methodName, getParamTypes(params));
method.invoke(object, params);
} public static Class<?>[] getParamTypes(Object[] params) {
Class<?>[] paramTypes = new Class<?>[params.length];
for (int i = 0; i < params.length; i++) {
paramTypes[i] = params[i].getClass();
} return paramTypes;
} public static void setProperty(Object object, String property, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = object.getClass().getDeclaredField(property);
field.setAccessible(true);
field.set(object, value);
}
}
### 回答2:
工具类Utils的设计如下:
1. 实现调用对象中指定方法名字和参数列表方法的调用:
```java
public class Utils {
public static Object invokeMethod(Object obj, String methodName, Object... args)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?>[] parameterTypes = new Class<?>[args.length];
for (int i = 0; i < args.length; i++) {
parameterTypes[i] = args[i].getClass();
}
Method method = obj.getClass().getMethod(methodName, parameterTypes);
return method.invoke(obj, args);
}
}
```
2. 实现给指定对象任意属性的赋值操作:
```java
import java.lang.reflect.Field;
public class Utils {
public static void setProperty(Object obj, String propertyName, Object value)
throws NoSuchFieldException, IllegalAccessException {
Field field = obj.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
field.set(obj, value);
}
}
```
使用示例:
```java
public class MyClass {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
try {
Utils.invokeMethod(obj, "setName", "John");
Utils.invokeMethod(obj, "setAge", 25);
Utils.setProperty(obj, "name", "Peter");
Utils.setProperty(obj, "age", 30);
System.out.println(Utils.invokeMethod(obj, "getName")); // Output: Peter
System.out.println(Utils.invokeMethod(obj, "getAge")); // Output: 30
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
以上代码演示了如何使用Utils工具类来调用对象中指定方法的方法,以及给指定对象的属性赋值的操作。
### 回答3:
工具类Utils:
1. 能实现调用对象中指定方法名字和参数列表方法的调用:
```
import java.lang.reflect.Method;
public class Utils {
public static Object invokeMethod(Object object, String methodName, Object... args) throws Exception {
Class<?> clazz = object.getClass();
Method method = clazz.getMethod(methodName, getParameterTypes(args));
return method.invoke(object, args);
}
private static Class<?>[] getParameterTypes(Object... args) {
Class<?>[] parameterTypes = new Class<?>[args.length];
for (int i = 0; i < args.length; i++) {
parameterTypes[i] = args[i].getClass();
}
return parameterTypes;
}
}
```
示例:
```
public class MyClass {
public void sayHello(String name) {
System.out.println("Hello, " + name);
}
}
public class Main {
public static void main(String[] args) throws Exception {
MyClass myClass = new MyClass();
Utils.invokeMethod(myClass, "sayHello", "John");
}
}
```
输出结果为:"Hello, John"
2. 实现给指定对象任意属性的赋值操作:
```
import java.lang.reflect.Field;
public class Utils {
public static void setField(Object object, String fieldName, Object value) throws Exception {
Class<?> clazz = object.getClass();
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);
}
}
```
示例:
```
public class MyClass {
private String name;
public void printName() {
System.out.println("Name: " + name);
}
}
public class Main {
public static void main(String[] args) throws Exception {
MyClass myClass = new MyClass();
Utils.setField(myClass, "name", "John");
myClass.printName();
}
}
```
输出结果为:"Name: John"
阅读全文