是否可以在 Dart 中定义一个抽象的命名构造函数?
Is it possible to define an abstract named constructor in Dart?
我正在尝试创建一个抽象 class Firestorable
以确保 subclass 重写命名构造函数 fromMap(Map<String, dynamic> map)
代码如下所示...
abstract class Firestorable {
/// Concrete implementations will convert their state into a
/// Firestore safe [Map<String, dynamic>] representation.
Map<String, dynamic> toMap();
/// Concrete implementations will initialize its state
/// from a [Map] provided by Firestore.
Firestorable.fromMap(Map<String, dynamic> map);
}
class WeaponRange implements Firestorable {
int effectiveRange;
int maximumRange;
WeaponRange({this.effectiveRange, this.maximumRange});
@override
WeaponRange.fromMap(Map<String, dynamic> map) {
effectiveRange = map['effectiveRange'] ?? 5;
maximumRange = map['maximumRange'] ?? effectiveRange;
}
@override
Map<String, int> toMap() {
return {
'effectiveRange': effectiveRange,
'maximumRange': maximumRange ?? effectiveRange,
};
}
}
执行此操作时我没有收到任何错误,但是当我省略 fromMap(..)
构造函数的具体实现时,我也没有收到编译错误。
例如,下面的代码将编译而不会出现任何错误:
abstract class Firestorable {
/// Concrete implementations will conver thier state into a
/// Firestore safe [Map<String, dynamic>] representation.
Map<String, dynamic> convertToMap();
/// Concrete implementations will initialize its state
/// from a [Map] provided by Firestore.
Firestorable.fromMap(Map<String, dynamic> map);
}
class WeaponRange implements Firestorable {
int effectiveRange;
int maximumRange;
WeaponRange({this.effectiveRange, this.maximumRange});
// @override
// WeaponRange.fromMap(Map<String, dynamic> map) {
// effectiveRange = map['effectiveRange'] ?? 5;
// maximumRange = map['maximumRange'] ?? effectiveRange;
// }
@override
Map<String, int> convertToMap() {
return {
'effectiveRange': effectiveRange,
'maximumRange': maximumRange ?? effectiveRange,
};
}
}
我是否无法定义一个抽象的命名构造函数并将其作为具体的要求实现 class?如果不是,正确的做法是什么?
如 Dart 语言官方指南中所述constructors aren’t inherited,因此您不能强制使用工厂构造函数来子classes。为了确保实现,它应该是 class' 接口的一部分,而构造函数不是。您可以查看这些相关的 Whosebug 问题以获取更多信息:
How do i guarentee a certain named constructor in dart
我正在尝试创建一个抽象 class Firestorable
以确保 subclass 重写命名构造函数 fromMap(Map<String, dynamic> map)
代码如下所示...
abstract class Firestorable {
/// Concrete implementations will convert their state into a
/// Firestore safe [Map<String, dynamic>] representation.
Map<String, dynamic> toMap();
/// Concrete implementations will initialize its state
/// from a [Map] provided by Firestore.
Firestorable.fromMap(Map<String, dynamic> map);
}
class WeaponRange implements Firestorable {
int effectiveRange;
int maximumRange;
WeaponRange({this.effectiveRange, this.maximumRange});
@override
WeaponRange.fromMap(Map<String, dynamic> map) {
effectiveRange = map['effectiveRange'] ?? 5;
maximumRange = map['maximumRange'] ?? effectiveRange;
}
@override
Map<String, int> toMap() {
return {
'effectiveRange': effectiveRange,
'maximumRange': maximumRange ?? effectiveRange,
};
}
}
执行此操作时我没有收到任何错误,但是当我省略 fromMap(..)
构造函数的具体实现时,我也没有收到编译错误。
例如,下面的代码将编译而不会出现任何错误:
abstract class Firestorable {
/// Concrete implementations will conver thier state into a
/// Firestore safe [Map<String, dynamic>] representation.
Map<String, dynamic> convertToMap();
/// Concrete implementations will initialize its state
/// from a [Map] provided by Firestore.
Firestorable.fromMap(Map<String, dynamic> map);
}
class WeaponRange implements Firestorable {
int effectiveRange;
int maximumRange;
WeaponRange({this.effectiveRange, this.maximumRange});
// @override
// WeaponRange.fromMap(Map<String, dynamic> map) {
// effectiveRange = map['effectiveRange'] ?? 5;
// maximumRange = map['maximumRange'] ?? effectiveRange;
// }
@override
Map<String, int> convertToMap() {
return {
'effectiveRange': effectiveRange,
'maximumRange': maximumRange ?? effectiveRange,
};
}
}
我是否无法定义一个抽象的命名构造函数并将其作为具体的要求实现 class?如果不是,正确的做法是什么?
如 Dart 语言官方指南中所述constructors aren’t inherited,因此您不能强制使用工厂构造函数来子classes。为了确保实现,它应该是 class' 接口的一部分,而构造函数不是。您可以查看这些相关的 Whosebug 问题以获取更多信息:
How do i guarentee a certain named constructor in dart