在Dart语言中扩展具有额外功能的List类 [英] Extending base List class with extra functionality in Dart language

查看:581
本文介绍了在Dart语言中扩展具有额外功能的List类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This question is about Dart language. I want to have a class which is just a List but with some extra functionality.

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

For example I have a class named Model:

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



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

I know that Model's type could take only four values: from 0 to 3. And I want to have a method, which can give me a List of Models of specified type, e.g. List<Model> modelCollection.getByType(int type);. I plan to to have four 'hidden' Lists of the Models (grouped by type) in that class. Thus I need to override addition and removal of List elements to make that hidden lists being up to date.

我怎么能实现这尽可能容易?

How can I realize this as easy as possible?

PS我知道这是很简单,但我很不熟悉对象继承,不能找到合适的例子。
P.P.S.

P.S. I know this is quite simple, but I'm poorly familiar with Object inheritance and can't find proper examples. P.P.S. I've also checked this but don't know is it outdated or not and didn't catch the idea.

推荐答案

我已经检查过了,但不知道是不是过时了,使类实现列表有以下几种方法:

To make a class implement List there are several ways :

  • Extending ListBase and implementing length, operator[], operator[]= and length= :
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
}




  • Mixin ListMixin 并实施 length operator [] operator [] = length =

    • Mixin ListMixin and implementing length, operator[], operator[]= and length= :
    • 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
      }
      




      • 使用列表来自 DelegatingList nofollow> quiver package

        • Delegating to an other List with DelegatingList from the quiver package:
        • import 'package:quiver/collection.dart';
          
          class MyCustomList<E> extends DelegatingList<E> {
            final List<E> _l = [];
          
            List<E> get delegate => _l;
          
            // your custom methods
          }
          

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

          Depending on your code each of those options have their advantages. If you wrap/delegate an existing list you should use the last option. Otherwise use one of the two first options depending on your type hierarchy (mixin allowing to extend an other Object).

          这篇关于在Dart语言中扩展具有额外功能的List类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆