小编spa*_*ffy的帖子

为什么Java不允许在类外部初始化变量或常量?

示例代码:

public class MyClass {

    public static double globallyVisibleDbl = 42.69;
    public static final int globallyVisibleInt = 42;
    //blah blah rest of code

}
Run Code Online (Sandbox Code Playgroud)

并且在一个类中都有双精度类型的变量和常量。

但是,当我尝试将这两个外部类放到类之前(例如,在类之前和导入语句之后)时,就像在C或C ++中由于#define token [value]指令允许的那样,我得到了错误:

import com.something.*;

public static double globallyVisibleDbl = 42.69;
public static final int globallyVisibleInt = 42;

public class MyClass {
   //some code
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样呢?

java variables global initialization constants

3
推荐指数
1
解决办法
3512
查看次数

嵌套的If/Else控制流正在执行时不应该 - 逻辑操作错误

我有这个代码:

def time24hr(tstr):

    if ('a' and ':') in tstr:
        if tstr[:2] == '12':
            tstr = tstr.replace('12', '00').replace(':','').replace('am','hr')
            return tstr

        elif len(tstr[:tstr.find(':')]) < 2:
        # If length of string's 'tstr' slice (that ends before string ':') is less than 2
            tstr = tstr.replace(tstr[:tstr.find(':')],'0' + tstr[:tstr.find(':')]).replace(':', '').replace('am','hr')
            # Replace one-character string with combination of '0' and this one-character string. Then remove colon, by replacing it with lack of characters and replace 'am' string with 'hr'.
            return tstr

        else:
            tstr = tstr.replace(':', '').replace('am', …
Run Code Online (Sandbox Code Playgroud)

python string logic if-statement

1
推荐指数
1
解决办法
911
查看次数