条件Java编译 [英] Conditional Java compilation
问题描述
在Visual Studio中,我们有项目配置和#ifdef ;我知道Java中没有#ifdef,但也可能是文件级别的一些?
您可以设置'final'如果要编译器来优化编译的字节码。
...
public static final boolean myFinalVar =假;
...
if(myFinalVar){
do something ....
....
}
如果代码编译时'myFinalVar'为false,那么做某事....将被错过编译的类。如果你有不止一个条件 - 可以把它们整理一下:把它们全部转移到另一个类(比如'Config.myFinalVar'),然后这些条件都可以保存在一个整齐的位置。
这种机制在'Hardcore Java'。
[实际上我认为这是与前面发布的穷人的ifdef相同的机制。]
I'm a longtime C++ programmer, new to Java. I'm developing a Java Blackberry project in Eclipse. Question - is there a way to introduce different configuration sets within the project and then compile slightly different code based on those?
In Visual Studio, we have project configurations and #ifdef; I know there's no #ifdef in Java, but maybe something on file level?
You can set up 'final' fields and ifs to get the compiler to optimize the compiled byte-codes.
...
public static final boolean myFinalVar=false;
...
if (myFinalVar) {
do something ....
....
}
If 'myFinalVar' is false when the code is compiled the 'do something....' bit will be missed out of the compiled class. If you have more than one condition - this can be tidied up a bit: shift them all to another class (say 'Config.myFinalVar') and then the conditions can all be kept in one neat place.
This mechanism is described in 'Hardcore Java'.
[Actually I think this is the same mechanism as the "poor man's ifdef" posted earlier.]
这篇关于条件Java编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!