需要打印函数调用时使用的实参名
Need to print function's actual parameter name used while calling
我想在函数中打印函数实参名
供参考,请参阅下文code.Here我正在尝试反射。
class Refrction
{
public static int a=12;
public static int b=12;
public static int c=13;
public void click(int x)
{
Class cls=Refrction.class;
Field[] fields = cls.getFields();
//here i want to print "a" if function actual parameter is "a" while calling the click function
//here i want to print "b" if function actual parameter is "b" while calling the click function
//here i want to print "c" if function actual parameter is "c" while calling the click function
}
}
public class Reflections extends Refrction
{
public static void main(String[] args)
{
Refrction ab=new Refrction();
ab.click(a);
ab.click(b);
ab.click(c);
}
}
除非 a
、b
和 c
的值永远不会改变(并且您可以通过查看值来推断哪个变量被用作参数)这是不可能的。您需要向该方法传递更多信息。
一种方法是
public void click(int x, String identifier) {
...
}
并用
调用它
ab.click(a, "a");
或者,您可以将值包装在一个(可能可变的)对象中,如下所示:
class IntWrapper {
int value;
public IntWrapper(int value) {
this.value = value;
}
}
然后
public static IntWrapper a = new IntWrapper(11);
和
public void click(IntWrapper wrapper) {
if (wrapper == a) {
...
}
...
}
我想在函数中打印函数实参名
供参考,请参阅下文code.Here我正在尝试反射。
class Refrction
{
public static int a=12;
public static int b=12;
public static int c=13;
public void click(int x)
{
Class cls=Refrction.class;
Field[] fields = cls.getFields();
//here i want to print "a" if function actual parameter is "a" while calling the click function
//here i want to print "b" if function actual parameter is "b" while calling the click function
//here i want to print "c" if function actual parameter is "c" while calling the click function
}
}
public class Reflections extends Refrction
{
public static void main(String[] args)
{
Refrction ab=new Refrction();
ab.click(a);
ab.click(b);
ab.click(c);
}
}
除非 a
、b
和 c
的值永远不会改变(并且您可以通过查看值来推断哪个变量被用作参数)这是不可能的。您需要向该方法传递更多信息。
一种方法是
public void click(int x, String identifier) {
...
}
并用
调用它ab.click(a, "a");
或者,您可以将值包装在一个(可能可变的)对象中,如下所示:
class IntWrapper {
int value;
public IntWrapper(int value) {
this.value = value;
}
}
然后
public static IntWrapper a = new IntWrapper(11);
和
public void click(IntWrapper wrapper) {
if (wrapper == a) {
...
}
...
}