Java“自我”(静态)参考

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5271016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:21:08  来源:igfitidea点击:

Java "self" (static) reference

javastaticscopeself

提问by feffi

I am looking for a "self" reference to the current class in JAVA in a static context manner like in PHP Scope Resolution Operator?

我正在以静态上下文方式寻找对 JAVA 中当前类的“自我”引用,例如在PHP Scope Resolution Operator 中

Solution: Break out of scope? BEWARE, this is compared to a static definition reallyslow (by factor 300):

解决方案:超出范围?当心,这与静态定义非常慢(因子为 300)相比:

static Logger LOG = LoggerFactory.getLogger(new RuntimeException().getStackTrace()[0].getClassName());

The old-fashioned way would be:

老式的方法是:

static Logger LOG = LoggerFactory.getLogger(<Classname>.class.getName());

Are there any alternatives? I'm looking for a way to put the logger definition in an abstract class. The logger should determine the class it's being called from by itself.

有没有其他选择?我正在寻找一种将记录器定义放在抽象类中的方法。记录器应该自己确定它被调用的类。

回答by Peter Lawrey

The slightly faster

稍微快一点

static final Logger LOG = LoggerFactory.getLogger(
       Thread.currentThread().getStackTrace()[0].getClassName());

If you do this 1000 times it will take 36 ms using Class.class.getName() and 60 ms doing it this way. Perhaps its not worth worrying about too much. ;)

如果你这样做 1000 次,使用 Class.class.getName() 需要 36 毫秒,这样做需要 60 毫秒。也许它不值得担心太多。;)

回答by Stan Kurilin

You should not inherit logger. Just declare logger in each class.

你不应该继承记录器。只需在每个类中声明记录器。

But if you don't want do such useful think, just don't make it static)

但如果你不想做这样有用的思考,就不要让它成为静态的)

回答by Stephen C

I don't think there are any alternatives that are significantly different to the two in your question.

我认为没有任何替代方案与您问题中的两者有显着不同。

You could create a helper method like this:

你可以创建一个这样的辅助方法:

public static String getCallingClassname() {
    return new RuntimeException().getStackTrace()[1].getClassName();
}

and then

接着

static Logger LOG = LoggerFactory.getLogger(Helper.getCallingClassname());

but that's as expensive as the original version. (FWIW - 300 times as slow is probably not a major concern, unless you have thousands of these loggers. Each of these statics is initialized just once ...)

但这与原始版本一样昂贵。(FWIW - 慢 300 倍可能不是主要问题,除非你有数千个这样的记录器。这些静态中的每一个都只初始化一次......)

My personal preference is for the "old fashioned" way of doing it.

我个人更喜欢“老式”的做法。

回答by surendrapanday

You don't need to use any of those. What I find convenient to use is:
//Logger

您不需要使用其中任何一个。我觉得使用方便的是:
//Logger

private static final Logger logger = LoggerFactory.getLogger(CreateEmployeeRecord.class or this.class);

private static final Logger logger = LoggerFactory.getLogger(CreateEmployeeRecord.class or this.class);

Do it in every class that needs logging, and change class name in bracket to corresponding class wherever it's being used.

在每个需要记录的类中都这样做,并在使用它的地方将括号中的类名更改为相应的类。