代碼
提交代碼
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionDemo1 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
// 實(shí)例化字符串對象
String name = new String("Colorful");
// 獲取 method 對象
Method method = String.class.getMethod("replace", CharSequence.class, CharSequence.class);
// 調(diào)用 invoke() 執(zhí)行方法
String result = (String) method.invoke(name, "ful", "");
System.out.println(result);
}
}
運(yùn)行結(jié)果