【问题标题】:Extending base List class with extra functionality in Dart language用 Dart 语言扩展基 List 类的额外功能
【发布时间】:2013-08-29 19:57:11
【问题描述】:

这个问题是关于 Dart 语言的。 我想要一个只是一个列表但具有一些额外功能的类。

例如我有一个名为 Model 的类:

class Model{
  String name;
  int type;
  Model(this.name, this.type);
}

我知道 Model 的类型只能取四个值:从 0 到 3。 我想要一个方法,它可以给我一个指定类型的模型列表,例如List<Model> modelCollection.getByType(int type);。 我计划在该类中有四个“隐藏”模型列表(按类型分组)。 因此,我需要覆盖 List 元素的添加和删除,以使隐藏的列表保持最新。

我怎样才能尽可能简单地实现这一点?

附:我知道这很简单,但我对对象继承不太熟悉,找不到合适的例子。 附言我也检查过这个,但不知道它是否过时并且没有抓住这个想法。

【问题讨论】:

标签: class inheritance dart extend


【解决方案1】:

要让一个类实现List,有几种方法:

import 'dart:collection';

class MyCustomList<E> extends ListBase<E> {
  final List<E> l = [];
  MyCustomList();

  void set length(int newLength) { l.length = newLength; }
  int get length => l.length;
  E operator [](int index) => l[index];
  void operator []=(int index, E value) { l[index] = value; }

  // your custom methods
}
import 'dart:collection';

class MyCustomList<E> extends Base with ListMixin<E> {
  final List<E> l = [];
  MyCustomList();

  void set length(int newLength) { l.length = newLength; }
  int get length => l.length;
  E operator [](int index) => l[index];
  void operator []=(int index, E value) { l[index] = value; }

  // your custom methods
}
import 'package:quiver/collection.dart';

class MyCustomList<E> extends DelegatingList<E> {
  final List<E> _l = [];

  List<E> get delegate => _l;

  // your custom methods
}

根据您的代码,每个选项都有其优势。如果您包装/委托现有列表,则应使用最后一个选项。否则,根据您的类型层次结构使用前两个选项之一(mixin 允许扩展其他对象)。

【讨论】:

  • 谢谢,我试试第一个!
【解决方案2】:

一个基本的方法是用IterableMixin 扩展一个对象。似乎您甚至不需要覆盖“长度”getter,或者说 IterableMixin 已经提供的所有方法。

import 'dart:collection';    

class Model {
   String name;
   int type;

   Model(this.name, this.type) {
   }
}

class ModelCollection extends Object with IterableMixin {

   List<Model> _models;
   Iterator get iterator => _models.iterator;

   ModelCollection() {
      this._models = new List<Model>();
   }

   //get one or the first type
   Model elementByType(int type) {

     for (Model model in _models) {
       if (model.type == type) {
          return model;
       }
     }       
   }

   //get all of the same type
   List<Model> elementsByType(int type) {

      List<Model> newModel = new List<Model>();

      for (Model model in _models) {
         if (model.type == type) {
            newModel.add(model);
         }
      }
      return newModel;       
   }

   add(Model model) {
      this._models.add(model);
   }
}

请原谅我的强静态类型。

【讨论】:

  • 这种方法似乎最适合我的需要。我会尝试两者并分享我的最终方法。
  • 我更喜欢这个答案。更多细节,我喜欢强静态类型:)
【解决方案3】:

您可能对 quiver.dart 的 Multimap 感兴趣。它的行为类似于允许每个键有多个值的 Map。

这里是github上的代码:https://github.com/google/quiver-dart/blob/master/lib/src/collection/multimap.dart#L20

它在酒吧里就像箭袋一样。我们很快就会在某个地方托管 dartdocs。

【讨论】:

  • 在我的例子中,我需要完全扩展 List 类,因为 Web UI 模板迭代适用于 Lists,但有些棘手的 Maps。
  • 地图和 Multimap 也有一个可以迭代的 values 属性,所以 &lt;template iterate="{{ v in map.values }}"&gt; 可以工作。
  • 哦!好消息!谢谢!
  • 同时我发现了一些东西interesting
猜你喜欢
  • 2021-10-09
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
相关资源
最近更新 更多