Java中宏替换的替代方法 [英] Alternatives to macro substitution in java
问题描述
我有一条日志语句,在其中我始终使用 this.getClass()。getSimpleName()
作为第一个参数。
我想把它放在某种宏常量中,并在我的所有log语句中使用它。
但是我了解到Java没有像C ++这样简单的机制。
I have a log statement in which I always use this.getClass().getSimpleName()
as the 1st parameter.
I would like to put this in some sort of macro constant and use that in all my log statements.
But I learned that Java has no such simple mechanism unlike say C++.
在Java中实现这种功能的最佳方法是什么?
What is the best way to achieve this sort of functionality in Java?
我的示例日志语句(来自Android)如下。
My example log statements (from Android) is as follows..
Log.v(this.getClass().getSimpleName(),"Starting LocIden service...");
推荐答案
Java没有宏,但是您可以使宏代码更短:
Java doesn't have macros but you can make your code much shorter:
Log.v(this, "Starting LocIden service...");
在 Log
类中:
public void v(Object object, String s)
{
_v(object.getClass().getSimpleName(), s);
}
另一种方法可能是检查调用堆栈。
Another approach could be to inspect the call stack.
这篇关于Java中宏替换的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!