安卓/ Java的:调用使用反射的方法? [英] Android/Java: Calling a method using reflection?

查看:205
本文介绍了安卓/ Java的:调用使用反射的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态方法名为chooseDialog(的String,int i)以中,我想呼吁的基础上提供给chooseDialog参数相同的类(Dialogs.class)内的另一种方法。 s是所希望的方法的名称和i是它的单个参数。

I have a static method titled chooseDialog(String s, int i) in which I want to call another method within the same class (Dialogs.class) based on the parameters provided to chooseDialog. s is the name of the desired method and i is it's single parameter.

我已经试过无数的教程,并花了几个小时的主题读书了,但我似乎无法得到一个牢牢把握,究竟我需要做的。

I have tried numerous tutorials and have spent a few hours reading up on the subject but I can't seem to get a firm grasp as to what exactly I need to do.

任何想法?

谢谢!

推荐答案

为什么要调用一个方法的名称在一个字符串参数传递?你能不能创建一个常数不同的动作,然后用开关并在每种情况下与参数调用方法i

Why do you want to call a method with name passed in a String parameter? Cannot you create a constants for different actions, then use switch and in each case call the method with parameter i?

您将有编译器检查你的code错误的好处。

You will have the benefit of compiler checking your code for errors.

修改的:如果你真的想使用反射,检索方法对象:

edit: if you really want to use reflection, retrieve a Method object with:

Method m = YourClass.class.getMethod("method_name",new Class[] { Integer.class }) 

我想Integer.class可能会奏效。然后调用梅托德为

I guess Integer.class might work. Then invoke the metod as

m.invoke(null,123); //first argument is the object to invoke on, ignored if static method

这篇关于安卓/ Java的:调用使用反射的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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