我正在尝试实现一个持久的Stack
数据结构。我想将其实现为代数数据类型,因此它有两个具体的子类型:空和非空:
abstract class Stack<T> {
factory Stack.empty() => const _EmptyStack._();
T get data;
Stack<T> get bottom;
bool get isEmpty;
Stack<T> put(T item) => new _StackImpl(item, this);
}
class _StackImpl<T> extends Stack<T> {
final T _data;
final Stack<T> _bottom;
_StackImpl(T this._data, Stack<T> this._bottom);
T get data => _data;
Stack<T> get bottom => _bottom;
bool get isEmpty => false;
}
class _EmptyStack<T> extends Stack<T> {
const _EmptyStack._();
T get data => throw new CollectionIsEmpty();
Stack<T> get bottom => throw new CollectionIsEmpty();
bool get isEmpty => true;
}
此代码在具体实现中引发了两个错误:
[error] The class 'Stack' does not have a default generative constructor
我在这里找到了一个似乎可以解决这个问题的示例代码,所以我通过在Stack<T>
类中放置一个无参数构造函数来修复它:
abstract class Stack<T> {
Stack();
// ...
但是现在这会导致_EmptyStack<T>
构造函数出现问题,这是恒定的:
Constant constructor cannot call non-constant super constructor of 'Stack<T>'
此外,添加的Stack()
构造函数可防止将类用作 mixin。
这些限制似乎迫使班级作者考虑如何扩展班级。从包中扩展List
类dart:collection
的方式似乎证实了这个结论——有一个完整的单独的类用于扩展,我不能直接扩展List
类本身。
我的问题比上面描述的问题更笼统:我怎样才能编写一个类,以便它可以足够灵活地扩展?这包括允许使用以下功能:
- 超类中的工厂构造函数
- 子类中的普通构造函数
const
子类中的构造函数- 用作mixin
虽然我知道作为 mixin 的使用可能是不可能的,甚至是不需要的,但其他点仍然有效。最重要的问题是:为什么我不能extend
创建一个带有工厂构造函数的类?这是一种与我熟悉的任何其他 OO 语言不同的行为。
还有相关问题:
编辑:感谢Günter Zöchbauer 的回答,我改进了代码,所以现在它可以完全运行(见下文)。我现在留下的最重要的问题是:为什么工厂构造函数破坏了扩展类的能力?以及如何解决它(除了使用基类作为接口)?一个更简单的例子来说明这一点:
class Base {
}
class _Sub extends Base {
int someValue;
_Sub(int this.someValue);
}
这段代码一切都很好。但是假设我及时回到我的Base
班级并想添加工厂方法:
class Base {
factory Base.empty() => new _Sub(0);
}
现在每个扩展Base
的类都因为unresolved implicit call to super constructor
. 那我该怎么办?
来自原始问题的更正代码以供参考:
abstract class Stack<T> {
const Stack._();
factory Stack.empty() => const _EmptyStack._();
T get data;
Stack<T> get bottom;
bool get isEmpty;
Stack<T> put(T item) => new _StackImpl(item, this);
}
class _StackImpl<T> extends Stack<T> {
final T _data;
final Stack<T> _bottom;
_StackImpl(T this._data, Stack<T> this._bottom) : super._();
T get data => _data;
Stack<T> get bottom => _bottom;
bool get isEmpty => false;
}
class _EmptyStack<T> extends Stack<T> {
const _EmptyStack._() : super._();
T get data => throw new CollectionIsEmpty();
Stack<T> get bottom => throw new CollectionIsEmpty();
bool get isEmpty => true;
}
void main(){
group('stack', (){
test('empty stack', (){
var emptyStack = new Stack.empty();
expect(emptyStack.isEmpty, isTrue);
expect(() => emptyStack.data, throwsA(new isInstanceOf<CollectionIsEmpty>()));
expect(() => emptyStack.bottom, throwsA(new isInstanceOf<CollectionIsEmpty>()));
var emptyStack2 = new Stack.empty();
expect(emptyStack == emptyStack2, isTrue);
});
test('adding to stack', (){
var stack = new Stack<String>.empty().put("a").put("b").put("c");
expect(stack.data, equals('c'));
expect(stack.bottom.data, equals('b'));
expect(stack.bottom.bottom.data, equals('a'));
});
});
}