dart:class 构造函数被标记为 const 的含义是什么
dart: what is the meaning of class constructor being marked as const
所以我看到了这样的代码:
class Whatever {
final String name;
const Whatever(this.name);
}
构造函数被标记为 const
会改变什么?到底有没有效果?
我读过这个:
Use const for variables that you want to be compile-time constants. If
the const variable is at the class level, mark it static const.
(Instance variables can’t be const.)
但是对于class构造函数来说似乎没有意义。
- 构造函数不能有构造函数体。
- 所有成员都必须是最终成员,并且必须在声明时或通过构造函数参数或初始化列表进行初始化。
- 您可以使用这个 class 的实例,其中只允许常量(注释、可选参数的默认值,...)
- 您可以创建像
static const someName = const Whatever()
; 这样的常量字段
如果 class 没有 const 构造函数,则它不能用于初始化常量字段。我认为在构造函数中指定它是有意义的。您仍然可以在运行时使用 new Whatever()
创建实例或添加工厂构造函数。
另见
- Dartlang const constructor - how is it different to "regular" constructor
- Dart factory constructor - how is it different to “const” constructor
- How to make a factory constructor that returns as const value
- Why does Dart have compile time constants?
- How to write abstract class constructors so that it will be flexible for extending in sub classes
"old style"(仍然有效)枚举是如何使用 const 的一个很好的例子
如果您的 class 生成永不改变的对象,您可以使这些对象成为编译时常量。为此,定义一个 const
构造函数并确保所有实例变量都是最终的。
class ImmutablePoint {
const ImmutablePoint(this.x, this.y);
final int x;
final int y;
static const ImmutablePoint origin = ImmutablePoint(0, 0);
}
代码示例
修改 Recipe
class 使其实例可以是常量,并创建一个执行以下操作的常量构造函数:
具有三个参数:ingredients, calories
和 milligramsOfSodium
(按此顺序)。
使用 this
。自动将参数值分配给同名对象属性的语法。
是常数,在构造函数声明中Recipe
之前加上const
关键字。
class Recipe {
final List<String> ingredients;
final int calories;
final double milligramsOfSodium;
const Recipe(this.ingredients, this.calories, this.milligramsOfSodium);
}
所以我看到了这样的代码:
class Whatever {
final String name;
const Whatever(this.name);
}
构造函数被标记为 const
会改变什么?到底有没有效果?
我读过这个:
Use const for variables that you want to be compile-time constants. If the const variable is at the class level, mark it static const. (Instance variables can’t be const.)
但是对于class构造函数来说似乎没有意义。
- 构造函数不能有构造函数体。
- 所有成员都必须是最终成员,并且必须在声明时或通过构造函数参数或初始化列表进行初始化。
- 您可以使用这个 class 的实例,其中只允许常量(注释、可选参数的默认值,...)
- 您可以创建像
static const someName = const Whatever()
; 这样的常量字段
如果 class 没有 const 构造函数,则它不能用于初始化常量字段。我认为在构造函数中指定它是有意义的。您仍然可以在运行时使用 new Whatever()
创建实例或添加工厂构造函数。
另见
- Dartlang const constructor - how is it different to "regular" constructor
- Dart factory constructor - how is it different to “const” constructor
- How to make a factory constructor that returns as const value
- Why does Dart have compile time constants?
- How to write abstract class constructors so that it will be flexible for extending in sub classes
"old style"(仍然有效)枚举是如何使用 const 的一个很好的例子
如果您的 class 生成永不改变的对象,您可以使这些对象成为编译时常量。为此,定义一个 const
构造函数并确保所有实例变量都是最终的。
class ImmutablePoint {
const ImmutablePoint(this.x, this.y);
final int x;
final int y;
static const ImmutablePoint origin = ImmutablePoint(0, 0);
}
代码示例
修改 Recipe
class 使其实例可以是常量,并创建一个执行以下操作的常量构造函数:
具有三个参数:
ingredients, calories
和milligramsOfSodium
(按此顺序)。使用
this
。自动将参数值分配给同名对象属性的语法。是常数,在构造函数声明中
Recipe
之前加上const
关键字。class Recipe { final List<String> ingredients; final int calories; final double milligramsOfSodium; const Recipe(this.ingredients, this.calories, this.milligramsOfSodium); }