Title image
p1nk's blog



javalearn

现在终于知道反射的作用到底是什么了吧。

我们总不能让开发者给我们写好java.lang.Runtime类吧

所以说得通过反射去获取:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package reflectiondemo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestReflection {

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class RuntimeClass = Class.forName("java.lang.Runtime");
Method method = RuntimeClass.getMethod("getRuntime");
Object runtime = method.invoke(RuntimeClass);
RuntimeClass.getMethod("exec", String.class).invoke(runtime, "gnome-calculator");
}

}






内敛而不懦弱 寂静而有力量


© - p1nk的生命线 : 2001 ~ 2025 - Powered by Hexo