它是"反射"当我用getClass.getMethod(...)? [英] Is it "reflection" when I use getClass.getMethod(...)?

查看:656
本文介绍了它是"反射"当我用getClass.getMethod(...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些蓝牙应用程序,但我从来没有见过这种特殊的方式< /一>,直到最近。在这个例子中,他们使用 device.getClass()实现getMethod(createRfcommSocket,新的等级[] {int.class}); 这似乎我只是一个很长的路要走话说的BluetoothSocket BS = createRfcommSocket(...

I've written a few bluetooth apps now, but I had never seen this particular approach until recently. In the example they use device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); which seems to me a long way of just saying BluetoothSocket bs = createRfcommSocket(....

什么是他们的方法之间的差异

What is the difference between their approach

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
sock = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));

和我的

sock = createRfcommSocket(.....

还有一个原因是使用一个或其他?

Is there a reason to use one or the other?

感谢您

推荐答案

是的,使用getClass等的反映。

Yes, using getClass etc. is reflection.

不同的是,你从IDE和编译器的情况下,你输错方法的名称,或做出类似错误的帮助,而这code的作者都是靠自己。

The difference is that you get help from the IDE and the compiler in case you mistype the method name or make a similar mistake, while the authors of that code are on their own.

也许他们是在一个情况下调用该方法只是没有用在编译的时候,这可能是在软件方面的情况下,允许装载在运行时新的插件。但在这种情况下,你不希望看到的方法名称和其他细节很难codeD插入code。

Maybe they are in a situation where the method to call just isn't available at compile time, which might be the case in the context of software that allows loading in new plugins at runtime. But in that case you wouldn't expect to see the method name and other details hardcoded into the code.

你的榜样的文字文本只是看起来像编程可怜我。

The literal text of your example just looks like poor programming to me.

这篇关于它是&QUOT;反射&QUOT;当我用getClass.getMethod(...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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