小编acu*_*ner的帖子

为什么不能在内部类中使用初始化块作为静态变量?

为什么java不允许在非静态内部类中使用静态初始化块进行静态声明?

在下面的代码中,尽管做了同样的事情,outer2将工作,而inner2将不会.有任何想法吗?我不是在寻找一种解决方法,我只是想了解为什么java无法做到这一点.

public class WhyUNoStatic {
    public static final String outer1 = "snth";  // ok
    public static final String outer2;  // ok
    static
    {
        outer2 = "snth";
    }

    public class Inner {
        public static final String inner1 = "snth";  // still ok! 
        public static final String inner2;  // FAILURE TIME
        static
        {
            inner2 = "snth";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:请注意,这inner1将正常工作.并不是java禁止内部类中的静态变量,它只是禁止声明它们.

java static declaration inner-classes

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

迭代python 3中的枚举时,并不是所有元素都会出现

在下面的代码,我分配b给功能fEnum.然而,当我遍历那个枚举时,b虽然我仍然可以通过它访问它E.b.有谁知道这里发生了什么?这只是一个错误吗?我正在使用python 3.5.1.

In [42]: from enum import Enum
In [43]: def f(): pass
In [44]: class E(Enum):
    ...:     a = 4
    ...:     b = f
    ...:     c = 5
    ...:
In [45]: list(E)
Out[45]: [<E.a: 4>, <E.c: 5>]
In [46]: E.b
Out[46]: <function __main__.f>
Run Code Online (Sandbox Code Playgroud)

python enums python-3.x

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

标签 统计

declaration ×1

enums ×1

inner-classes ×1

java ×1

python ×1

python-3.x ×1

static ×1