继承自List [英] Inheriting from a List

查看:78
本文介绍了继承自List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个AugmentedStringList类,List< string>有几个成员描述整个列表的类。显然,我可以使用List< string>创建一个类。成员,并将来自AugmentedStringList的lsit上的所有操作(例如Add,Remove)映射到该List< string>会员。但是C#编译器允许我子类化List< string>,使所有这些操作直接可用:



I want to create an AugmentedStringList class, List<string> class with a few members describing the list as a whole. Obviously, I can make a class with a List<string> member, and map all operations on the lsit (such as Add, Remove) from AugmentedStringList to that List<string> member. But the C# compiler allows me to subclass List<string>, making all those operations directly available:

public class AugmentedStringList : List<string> {
  public string language {get; set; }

  public AugmentedStringList(string lang, string initialStrings) {
    language = lang;
    if (string.IsNullOrEmpty(initialStrings)) <baseconstructor>()
    else <baseconstructor>(initialStrings.Split(';');    
  }
}





还有更多的AugmentedStringList,但这说明了我的问题:如何引用基本列表< string>?不,我不能使用''这个设置列表值'';它是只读的,它也指增强结构,而不是基本字符串列表。也不能指定''base''。



如果我总是可以调用相同的基本构造函数,我当然可以按照普通方式执行,添加:base(...),但在这种情况下不起作用。还有很多其他操作,其中我需要直接参考简单列表,''这个''不起作用。



有什么方法可以参考简单的字符串列表从类方法,或者我必须使用List< string>所有相关操作的成员和写存根?



There is more to the AugmentedStringList, but this illustrates my problem: How do I refer to the base List<string>? No, I cannot set the list value using ''this''; it is read only, and it also refers to the augmented structure, not the base string list. Nor can I assign to ''base''.

If I could always call the same base constructor, I could of course do it the ordinary way, adding : base(...), but that doesn''t work in this case. There are lots of other operations, too, where I need to refer directly to the simple list, and ''this'' doesn''t work.

Is there any way I can refer to the simple string list from the class methods, or do I have to use a List<string> member and write stubs for all the relevant operations?

推荐答案

这个适用于我能想到的所有情况。例如,您可以通过索引直接访问列表,或使用基类方法:

this works in all cases I can think of. For example, you can access the list directly by index, or use the base class methods:
public class MyList : List<string>
    {
    public string GetAt(int index)
        {
        return this[index];
        }
    public void AddMe(string s)
        {
        Add(s);
        }
    }



你想做什么不行?





在我的第一个描述中说明了我想做的事情之一:对构造函数参数进行一些处理以初始化列表成员。



另一件事(我的代码中未说明)是提取列表元素本身,没有增强(以额外参数的形式),如AugmentedStringList.GetSimpleList()函数,类似于AugmentedStringList.GetLanguage()。如何编写GetSimpleList() - 如何引用基类列表?






你不能从继承的类构造函数中调用基础构造函数 - 在执行继承的构造函数代码之前必须完成基础构造,或者它可以尝试使用未初始化的内存。所以你要做的就是做不了。但是,您可以在继承的类中创建两个构造函数(一个具有单个参数,一个具有两个),它们适当地引用基础构造函数:


What are you trying to do that doesn''t work?


"One of the things I would like to do is illustrated in my first description: Do some processing of constructor parameters to initialize the list member.

Another thing (not illustrated in my code) is to extract the list element itself, without the augmentations (in the form of extra parameters), like an AugmentedStringList.GetSimpleList() function, similar to AugmentedStringList.GetLanguage(). How do I write GetSimpleList() - how can I refer to the base class list?"



You can''t call a base constructor from within the inherited class constructor - the base construction has to be complete before the inherited constructor code is executed, or it could try to use uninitialized memory. So what you are trying to do can''t be done. But, you could create two constructors in the inherited class (one with a single param, and one with two) that reference the base constructors appropriately:

public string Lang { get; set; }
public MyList(string lang)
    {
    Lang = lang;
    }
public MyList(string lang, string initialStrings)
    : base(initialStrings.Split(';'))
    {
    Lang = lang;
    }



返回基类很简单:您的类实例是基类的实例:


To return the base class is easy: your class instance is an instance of the base class:

public List<string> GetSimpleList()
    {
    return this;
    }

但说实话,你不需要这样做,因为:

But to be honest you don''t need to do that, because:

MyList ml = new MyList("English", "hello;hello again");
List<string> ls = ml;

无论如何都会正常工作!

Will work fine anyway!


这篇关于继承自List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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