首页 > 解决方案 > 接受具体类实例的方法

问题描述

我有一个Filter由各种事务过滤器(例如,等)实现DateFilterAccountFilter抽象类

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 没有考虑DateFilterAccountFilter等价于TransactionFilter.

编辑:我希望>运算符严格由相同子类型的类使用(例如比较两个DateFilter实例)。在给定的示例中如何执行此操作?

标签: dart

解决方案


您正在做的事情没有类型安全的意义。您TransactionFilter指定所有继承自的类TransactionFilter都有一个>运算符,该运算符可以与 type 的任何对象进行比较TransactionFilter

因此,当您在子类中将运算符定义为:

bool operator >(DateFilter other)

或者:

bool operator >(AccountFilter other)

这是不允许的,因为DateFilter并且AccountFilterTransactionFilter(您的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();
  }
}

推荐阅读