然后调用方法将基类放在ArrayList中 [英] Call methods then base Class in ArrayList
本文介绍了然后调用方法将基类放在ArrayList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要帮助,我有这样的消息来源:
Need help, I have this sources:
public MainDB : List<string>
{
List<string> X;
List<string> Y;
public MainDB()
{
X = new List<string>();
Y = new List<string>();
...??? A ???... //suppose base = new List<string>(); but doesnt works
}
public ...?? B ??... void Add(string Value) //Maybe void abstract or just void or anything, to avoid the conflicts between the base's List method and current method
{
X.Add(Value);
Y.Add(Value);
...??? C ???... //suppose base.Add(Value); but doesnt works
}
}
任何人都知道如何解决A,B,和C? A,B和C是基数列表< string>我不知道如何...另外,A假设是这样的,但它不起作用:
Anyone know how to solve the A, B, and C? A, B, and C are the base's List<string> I dont know how... Plus, the A suppose to be like this, but it doesnt work :
base = new List<string>();
这里有一些例子:
Here some example:
MainDB test = new MainDB();
test.Add("hi");
test.Add("hello");
Console.WriteLine(test.X[0].ToString());
Console.WriteLine(test.Y[0].ToString());
Console.WriteLine(test[0].ToString());
Console.WriteLine(test.X[1].ToString());
Console.WriteLine(test.Y[1].ToString());
Console.WriteLine(test[1].ToString());
结果将是:
So the result will be:
hi
hi
hi
hello
hello
hello
推荐答案
不完全确定你在这里问的是什么,除了你正在扩展
Not totally sure what you are asking here, except that you are extending the
List<string>
类,并希望基本上有3个列表。
我想如果这将用于其他应用程序你应该扩展
class and want to basically have 3 lists in one.
I think if this is going to be used in other applications you should extend the
List<T>
类来支持泛型类型。
class to support generic types.
public MainDB : List<string>
{
private List<string> m_X;
private List<string> m_Y;
// Should have these properties and make the fields private
public List<string> X {
get {
return m_X;
}
}
public List<string> Y {
get {
return m_Y;
}
}
/* By calling this constructor, technically the base constructor is already called. If you want to show it being called add the base() call after MainDB() like so*/
public MainDB():base()
{
m_X = new List<string>();
m_Y = new List<string>();
}
/* The List<t> and equally List<string> does not support overriding Add so you must specify the new keyword to essentially hide the base Add.*/
public new void Add(string Value)
{
m_X.Add(Value);
m_Y.Add(Value);
base.Add(Value);
}
}
这篇关于然后调用方法将基类放在ArrayList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文