我可以打印出变量的名称吗? [英] Can I print out the name of the variable?

查看:114
本文介绍了我可以打印出变量的名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了一个号码。常量变量超过1000,这些常量是唯一整数。

I have created a no. of constant variables, more than 1000, those constants are unique integer.

public static final FOO  335343
public static final BAR  234234
public static final BEZ  122424
....
....
....

有没有办法打印出FOO,BAR和BEZ,这是Java中名字的变量?
我不熟悉java反射。我不知道是否有帮助。

Is there a way to print out the FOO, BAR and BEZ, the variable of the names in Java? I am not familiar with java reflection. I don't know if that helps.

if ( FOO == 335343)
   ---> output "FOO"
if ( BAR == 234234 )
   ---> ouptut "BAR"
....

实际上问这个问题是我想要的将日志写入文件

Actually asking this question behind is that I want to write log into the file

System.out.println("This time the output is " + FOO);

且实际产量为

This time the output is 335323

我想知道哪个变量来了来自335323.
除了将这些变量及其关联常量放入hashMap之外还有其他方法吗?

I want to know which variable comes from 335323. Is there any other way apart from putting those variable and its associate constant into hashMap?

谢谢

推荐答案

有一些'特殊情况'你可以解决这个问题(由其他人讲述),但最重要的问题是:你为什么要做这个(打印出变量名)?

There are some 'special case' that u can have workaround for this (which is told by other), but the most important question is: why would you want to do this (printing out variable name)?

根据我的经验,99.9%的类似问题(如何打印变量名?如何获取变量取决于用户输入变量名?等)实际上是由初学者提出的编程,他们只是做了不正确的假设和设计。他们正在努力实现的目标可以通过更合适的设计来实现。

From my experience, 99.9% of similar questions (how to print variable name? how to get variable depends on user inputting variable name? etc) is in fact raised by beginner of programming and they simply have made incorrect assumptions and designs. The goal they are trying to achieve normally can be done by more appropriate design.

编辑

Edit

老实说,我仍然不认为你要做的是要走的路,但至少我认为以下是可行的回答:

Honestly I still do not think what you are trying to do is the way to go, but at least I think the following is a workable answer:

它或多或少是之前答案的组合:

It is more or less a combination of previous answer:

(没有尝试编译但至少它给你一个想法)

(Haven't try to compile but at least it give u an idea)

class Constants {
  public static final int FOO = 123;
  public static final int BAR = 456;

  private static Map<Integer, String> constantNames = null;


  public static String getConstantName(int constVal) {
    if (constantNames == null) {
      Map<Integer, String> cNames = new HashMap<Integer, String>()
      for (Field field : MyClass.class.getDeclaredFields()){
        if ((field.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) != 0) {
            && int.class == field.getType()){
          // only record final static int fields
          cNames.put((Integer)field.get(null), field.getName());
        }
      }
      constNames = cNames;
    }
    return constantNames.get(constVal);
  }
}

假设您想获得一个常量名称,只需执行:

assuming you want to get a constant name, just do:

Constants.getConstantName(123);  // return "FOO"

这篇关于我可以打印出变量的名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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