java反射 System.out.println [英] java reflection System.out.println

查看:56
本文介绍了java反射 System.out.println的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Java 反射.我试过了

<前>System.exit(3);Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3);

它的工作原理.我也成功跑了

<前>System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null));

现在如何反射调用 System.out.println

<前>java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!");

给出错误.我知道 System 没有 out 功能.所以建议一种反射调用 System.out.println 的方法

这是完整的例子

public class ReflectionDemo1 {public static void main(String[] args) 抛出异常 {//java.lang.System.exit(3);//java.lang.Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3);//java.lang.System.currentTimeMillis()//System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null));//System.out.println("嗨!");java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!");}}

解决方案

outjava.lang.Systemstatic 字段班级.

你可以使用Field类来引用它

systemClass = java.lang.Class.forName("java.lang.System");Field outField = systemClass.getDeclaredField("out");

该字段的类型是 PrintStream.您实际上不需要知道这一点,但您确实需要为它检索相应的 Class 对象.

printStreamClass = outField.getType();

我们知道它有一个 println(String) 方法,所以我们也可以检索它

Method printlnMethod = printStreamClass.getDeclaredMethod("println", String.class);

现在,由于 println(String) 是一个实例方法,我们需要在实例上调用它.哪个实例?out 字段引用的那个.out 字段是 static,因此我们可以通过将 null 传递给 Field#get(object).

Object object = outField.get(null);

然后我们调用方法

printlnMethod.invoke(object, "Hi!");

I m learning java reflection. I tried

System.exit(3);
Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3);

and it works. I successfully also ran

System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null)); 

Now how can i invoke System.out.println reflectively

java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!"); 

is giving error. I know System does not have out function. So suggest a way to call System.out.println reflectively

Here is the complete example

public class ReflectionDemo1 {
public static void main(String[] args) throws Exception {
// java.lang.System.exit(3);
// java.lang.Class.forName("java.lang.System").getMethod("exit", Integer.TYPE).invoke(null, 3);
// java.lang.System.currentTimeMillis()
// System.out.println(Class.forName("java.lang.System").getMethod("currentTimeMillis").invoke(null));
// System.out.println("Hi!");
java.lang.Class.forName("java.lang.System").getMethod("out.println", String.class).invoke(null, "Hi!");
}
}

解决方案

out is a static field of the java.lang.System class.

You can use the Field class to refer to it

Class<?> systemClass = java.lang.Class.forName("java.lang.System");
Field outField = systemClass.getDeclaredField("out");

The type of that field is PrintStream. You don't need to know that really, but you do need to retrieve the corresponding Class object for it.

Class<?> printStreamClass = outField.getType();

We know it has a println(String) method, so we can retrieve that too

Method printlnMethod = printStreamClass.getDeclaredMethod("println", String.class);

Now, since println(String) is an instance method, we need to invoke it on an instance. Which instance? The one referenced by the out field. That out field is static so we can get it by passing null to Field#get(object).

Object object = outField.get(null);

Then we invoke the method

printlnMethod.invoke(object, "Hi!");

这篇关于java反射 System.out.println的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆