简体   繁体   English

您能否将派生类添加到其基类列表中,然后从C#中的基类列表中调用派生类的方法

[英]Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class(possibly by casting it back to the Derived class since you know it was originally a Derived class) 您能否将派生类添加到其基类的列表中,然后从基类列表中调用派生类的方法(可能通过将其强制转换回派生类,因为您知道它最初是派生类)

public class MySystem
{
    public string name;

    MySystem(string name)
    {
        this.name = name;
    }

    public void Update()
    {
        //dostuff
    }
}

public class PowerSystem : MySystem
{
    public int totalPower;

    PowerSystem (string name, int power) : base(name)
    {
        this.totalPower = power;
    }

    public void Update()
    {
        base.Update();
        //Do other stuff
    }
}

void Main()
{
    List<MySystem> SystemList = new List<MySystem>();

    SystemList.Add(new System("Shields"));
    SystemList.Add(new System("Hull"));

    Power p = new Power("Power", 10);
    SystemList.Add(p);

    foreach(MainSystems ms in SystemList)   
    {
        if(ms.name != "Power")
            ms.Update();
        else
            (PowerSystem)ms.Update();   //This doesn't work
    }

}

So what I'm trying to do is run the update method for every element in the list, with the exeption of the one I named power and instead run the Power.Update method. 因此,我要尝试的是对列表中的每个元素运行update方法,并以我命名为power的那个元素的实例运行,而是运行Power.Update方法。

The closest post I have found to answering this is here unfortunately I don't fully understand it. 不幸的是,我发现最接近我的答案的帖子是这里

I'm hoping that the list is holding a reference to PowerSystem p and that somehow I can convert it and access the PowerSystem menthod. 我希望列表中包含对PowerSystem p的引用,并且希望以某种方式可以将其转换并访问PowerSystem方法。

I hope this is clear. 我希望这很清楚。 Thanks 谢谢

PS if you have a better idea for this I'm all ears. 附言:如果您对此有更好的主意,我将不胜感激。

Use polymorphism - mark Update in base class virtual and override it in derived class. 使用多态 -在基类virtual标记Update ,并在派生类中override它。

Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. 基类可以定义和实现虚拟方法,派生类可以覆盖它们,这意味着它们提供了自己的定义和实现。 At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. 在运行时,当客户端代码调用该方法时,CLR查找对象的运行时类型,并调用该虚拟方法的覆盖。 Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed. 因此,在源代码中,您可以在基类上调用方法,并使派生类的方法版本得以执行。

public class MySystem
{
    public string name;

    MySystem(string name)
    {
        this.name = name;
    }

    public virtual void Update()
    {
        //dostuff
    }
}

public class PowerSystem : MySystem
{
    public int totalPower;

    PowerSystem (string name, int power) : base(name)
    {
        this.totalPower = power;
    }

    public override void Update()
    {
        base.Update();
        //Do other stuff
    }
}

Now, PowerSystem.Update() will get called automatically 现在, PowerSystem.Update()将自动被调用

foreach(MainSystems ms in SystemList)   
{
    ms.Update();
}

For MySystem instances it will call MySystem.Update , but for PowerSystem instances the override will be called. 对于MySystem实例,它将调用MySystem.Update ,但是对于PowerSystem实例,将调用覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM