开发者

Type casting question

开发者 https://www.devze.com 2023-04-05 09:49 出处:网络
foreach(PlayerBase p in Members) { p.Render(); } The Members list contains members of type FieldPlayer or GoalKeeper. FieldPlayer an开发者_Python百科d GoalKeeper inherit from PlayerBase. When I call
foreach(PlayerBase p in Members)
{
    p.Render();
}

The Members list contains members of type FieldPlayer or GoalKeeper. FieldPlayer an开发者_Python百科d GoalKeeper inherit from PlayerBase. When I call p.Render(), I need the appropriate Render call to be called for FieldPlayer or GoalKeeper. How do I do that?

Thanks


You make the PlayerBase.Render method virtual. See this question for more information.


You're talking about Polymorphism. The proper implementation of Render is automatically called. Make sure the Render method in your PlayerBase class is defined as either abstract or virtual.


If FieldPlayer and GoalKeeper override the Render method, then this will happen automatically.

public abstract class PlayerBase
{
    public virtual void Render();
}

public class FieldPlayer : PlayerBase
{
    public override void Render() {...}
}


If PlayerBase is an interface

public inteface IPlayerBase 
{
   void Render();
}

public class FieldPlayer : IPlayerBase
{
   public void Render()
   {
      MessageBox.Show("FieldPlayer.Render");
   }
}

public class GoalKeeper : IPlayerBase
{
   public void Render()
   {
      MessageBox.Show("GoalKeeper.Render");
   }
}

If PlayerBase is an abstract class

public abstract class PlayerBase 
{
   public abstract void Render();
}

public class FieldPlayer : PlayerBase
{
   public override void Render()
   {
      MessageBox.Show("FieldPlayer.Render");
   }
}

public class GoalKeeper : PlayerBase
{
   public override void Render()
   {
      MessageBox.Show("GoalKeeper.Render");
   }
}

If PlayerBase is an class with a virtual function

public class PlayerBase 
{
   public virtual void Render()
   {
      MessageBox.Show("PlayerBase.Render");
   }
}

public class FieldPlayer : PlayerBase
{
   public override void Render()
   {
      MessageBox.Show("FieldPlayer.Render");
   }
}

public class GoalKeeper : PlayerBase
{
   public override void Render()
   {
      MessageBox.Show("GoalKeeper.Render");
   }
}

For all three instances, the derived type's Render function will be called.


If Render is a virtual member, then the method called will be that on the most derived type of the object at runtime.


You should declare Render on PlayerBase to be either virtual (if you have a default implementation) or abstract. Then, you override the method in the child classes with the desired behavior.


You can mark Render as abstract and then every derived class must implement it.

See this for more information: http://msdn.microsoft.com/en-us/library/ms173152%28v=vs.80%29.aspx


abstract class PlayerBase
{
    public abstract void Render();
}

Any class that inherits PlayerBase will be required to implement the Render function. If you have common functionality for Render, you can instead:

abstract class PlayerBase
{
    public virtual void Render() {
        // add common functionality
    }
}

Then in your sub-class, say FieldKeeper:

class FieldKeeper : PlayerBase    {
    public override void Render() {
        // add additional functionality
        base.Render();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号