我已经看过其他一些关于SO的类似问题,但它们似乎没有具体解决以下问题。
我想要实现的是拥有无法更改的编译时常量。
我有一个程序,我稍微重组了一下以消除混乱。该程序在"main()"之前有一些常量声明。我将它们移到了一个类中,但是它要求我将它们声明为"静态常量"。然后我想,好吧,"main()"之前的其他"const"声明可能也应该是"静态const"。然而,当我尝试这样做时,编辑建议"顶级声明不能声明为'静态'"。例如。
static const int I_CORRECT_YN = 12; // prompt nr.
所以,我有点困惑。我认为"常量"是静态的。为什么我必须在类中声明"静态"?为什么我不能将"顶级"常量声明为"静态"?另外,两者之间有什么区别:
static const int I_CORRECT_YN = 12;
const int I_CORRECT_YN = 12;
static final int I_CORRECT_YN = 12;
final int I_CORRECT_YN = 12; ?
声明无法更改的编译时值的最佳或唯一方法是什么?
我想我正在看字面意思,但我认为还有一个更复杂的含义。
为什么我必须在类中声明"静态"?
因为实例变量/方法无法const
。这意味着它们的值可能因实例而异,而编译时常量则不是这种情况。(来源)
为什么我不能将"顶级"常量声明为"静态"?
static
修饰符将变量/方法标记为类范围(类的每个实例的值相同)。顶级内容是应用程序范围的,不属于任何类,因此将它们标记为类范围没有任何意义,也是不允许的。(来源)
声明无法更改的编译时值的最佳或唯一方法是什么?
您已经在这样做了 - 在定义类常量时添加static
。定义顶级常量时不要添加它。另外,请使用 const
. final
变量不是编译时值。
[省略具有不同常量定义的源代码]有什么区别
static const
和const
几乎相同,用法取决于上下文。const
和 final
之间的区别在于const
是编译时常量 - 它们只能使用文本值(或运算符和文本值的表达式)进行初始化,并且不能更改。 final
变量初始化后也无法更改,但它们基本上是普通变量。这意味着可以使用任何类型的表达式,并且每个类实例的值可以是不同的:
import "dart:math";
Random r = new Random();
int getFinalValue() {
return new Random().nextInt(100);
}
class Test {
// final variable per instance.
final int FOO = getFinalValue();
// final variable per class. "const" wouldn't work here, getFinalValue() is no literal
static final int BAR = getFinalValue();
}
// final top-level variable
final int BAZ = getFinalValue();
// again, this doesn't work, because static top-level elements don't make sense
// static final int WAT = getFinalValue();
void main() {
Test a = new Test();
print(Test.BAR);
print(BAZ); // different from Test.BAR
print(a.FOO);
a = new Test();
print(Test.BAR); // same as before
print(BAZ); // same as before
print(a.FOO); // not the same as before, because it is another instance,
// initialized with another value
// but this would still be a syntax error, because the variable is final.
// a.FOO = 42;
}
我希望这有所帮助,我没有把它描述得太混乱。