在作为父级调用时执行子类方法 [英] performing a child classes method when called as a parent

查看:69
本文介绍了在作为父级调用时执行子类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Class A类和一个B类。
B类是A类的子代,所以:

I have a class Class A, and a class Class B. class B is a child of class A so that:

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,但是将从代码中的多个位置进行访问:

So I'm looking for a way to call getAData() whenever an object of B is called as A for example the list Xlist stores all As and Bs but will be accessed from several places in the code:

   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而不是其自身。

in the above instance I would like for every time an object is pulled from Xlist if its a B but gets caste as an A like so, it automatically calls the getAData() function and returns the resulting A instead of its self. is this possible??

推荐答案

您可以在父类 virtual 中创建该方法,并在子类中对其进行覆盖。类型A的实例,它将调用方法

You can make the method in parent class virtual and override it in the child classes. In doing so anywhere you call the method on an instance of type A, it will invoke the method in the derived type if the derived type provides and override, otherwise it will invoke the version in type A.

这是最简单的方法,替代方法不是很简单有吸引力。有关C#中的虚拟方法的更多信息,请查看此msdn文章;有关本文的更多信息,请参见参考。 http://msdn.microsoft.com/zh- us / library / aa645767(v = vs.71).aspx

This is the simplest way, the alternative aren't very attractive. For more information on virtual methods in C# check out this msdn article; http://msdn.microsoft.com/en-us/library/aa645767(v=vs.71).aspx

要做您想做的事情(我很确定这不是您实际上可以执行此操作);

To do what you think you want to do (I'm pretty sure it's not actually what you want to do) you can do this;

   for(int fileIdx = Xlist.Values.Count-1; fileIdx >= 0; --fileIdx)
   {
       A rec = (A)Xlist.GetByIndex(fileIdx);
       if (rec.GetType() == typeof(B))
       {
            B temp = (B) rec;
            rec = temp.getAData();
       }
   } 

尽管如此,这完全没有意义。这是一个例子;

Although, again, this makes no sense at all. Here's an example;

public class Car
{
     int year;
     bool manual;
}

public class Porsche : Car
{
    bool specialPorscheOnlyFeature;
    Engine enginge;
}

public class Engine
{
   string engineType;
} 
// in some method

Porsche p = new Porsche();
// to get Car data
int yearOfCar = p.year; 
bool isManual = p.manual;
bool specialFeature = p.SpecialPorscheOnlyFeature;

以上是继承工作方式的一个示例。我没有检索基类的实例,基类的所有内容都被烘焙到派生类的实例中。您的行为就像基类是派生类组成的其他对象一样。

Above is an example of how inheritance works. I don't retrieve an instance of the base class, everything the base class has is baked into the instance of the derived class. You're acting like the base class is some other object the derived class is composed of.

这篇关于在作为父级调用时执行子类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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