我有一个 A 类和一个 B 类。 B 类是 A 类的孩子,因此:
public class Class A
{
public DateTime FileStart
{
get
{
return Header.StartTime;
}
set{ }
}
...
...
}
和
public class B : A
{
FileInfo zippedA;
public A myA = null;
internal B(FileInfo mFileInfo)
{
...
//collects the same data as A from the fileinfo such as start time...
...
}
public A getAData()
{
UnZipFile(zippedA);
return myA;
}
...
}
因此,我正在寻找一种在调用getAData()
对象时B
调用的方法A
,例如列表 Xlist 存储所有 As 和 B,但将从代码中的多个位置访问:
SortedList Xlist = new SortedList();
public void GetFrames(DateTime desiredStartTime, DateTime desiredEndTime)
{
for(int fileIdx = Xlist.Values.Count-1; fileIdx >= 0; --fileIdx)
{
//my hope is that there is a way to set up B in it's class to say
// "if I get called as an A, I'll perform getAData() and return myA instead.
A rec = (A)Xlist.GetByIndex(fileIdx);
...
...
}
}
在上面的例子中,我希望每次从 Xlist 中拉出一个对象,如果它是 B 但像这样被种姓为 A,它会自动调用getAData()
函数并返回结果 A 而不是它自己。这可能吗??