dart - 接受具体类实例的方法
问题描述
我有一个Filter
由各种事务过滤器(例如,等)实现DateFilter
的AccountFilter
抽象类
abstract class TransactionFilter {
String asString();
bool operator >(TransactionFilter other);
}
// Concrete implementations
class DateFilter implements TransactionFilter {
DateTime startDate, endDate;
DateFilter(this.startDate, this.endDate);
// ⏹ ERROR
// 'DateFilter.>' ('bool Function(DateFilter)') isn't a valid override of
// 'TransactionFilter.>' ('bool Function(TransactionFilter)')
bool operator >(DateFilter other) =>
startDate.isBefore(other.startDate) && endDate.isAfter(other.endDate);
}
class AccountFilter implements TransactionFilter {
List<int> accounts;
DateFilter(this.accounts);
// Same error as above
bool operator >(AccountFilter other) =>
other.any((e) => !accounts.contains(e))
}
这似乎是因为 Dart 没有考虑DateFilter
和AccountFilter
等价于TransactionFilter
.
编辑:我希望>
运算符严格由相同子类型的类使用(例如比较两个DateFilter
实例)。在给定的示例中如何执行此操作?
解决方案
您正在做的事情没有类型安全的意义。您TransactionFilter
指定所有继承自的类TransactionFilter
都有一个>
运算符,该运算符可以与 type 的任何对象进行比较TransactionFilter
。
因此,当您在子类中将运算符定义为:
bool operator >(DateFilter other)
或者:
bool operator >(AccountFilter other)
这是不允许的,因为DateFilter
并且AccountFilter
比TransactionFilter
(您的DateFilter
班级只允许与另一个班级进行比较DateFilter
而不是 eg AccountFilter
)更具限制性。
更新 这是一个如何使用泛型代替的示例:
abstract class TransactionFilter<T extends TransactionFilter<T>> {
String asString();
bool operator >(T other);
}
// Concrete implementations
class DateFilter implements TransactionFilter<DateFilter> {
DateTime startDate, endDate;
DateFilter(this.startDate, this.endDate);
bool operator >(DateFilter other) =>
startDate.isBefore(other.startDate) && endDate.isAfter(other.endDate);
@override
String asString() {
// TODO: implement asString
throw UnimplementedError();
}
}
class AccountFilter implements TransactionFilter<AccountFilter> {
List<int> accounts;
AccountFilter(this.accounts);
bool operator >(AccountFilter other) =>
other.accounts.any((e) => !accounts.contains(e));
@override
String asString() {
// TODO: implement asString
throw UnimplementedError();
}
}
推荐阅读
- spring-boot - 无法访问 docker 容器中的 REST 端点
- c# - 任务/提交没有被调用
- javascript - 下一行应该只在上面的行完全执行时才执行 请记住在 html 中调用了该函数
- java - Nifi 自定义处理器显示错误“无法分配局部变量流文件”
- python - 如何制作按国家分组的子图
- storing-data - 在 mysquel 中存储数据的挑战
- java - 例外:实体映射中的重复列:列:哈希(应使用 insert="false" update="false" 映射)
- python - 在 Pandas 中转换数据类型
- windows - 无法在 IIS Express 上以前使用的端口上运行调试
- javascript - 使用 selenium python 修改 html 源代码的 TagName