从派生类调用基本构造函数时不确定参数 [英] Unsure about parameters when calling base constructor from derived class

查看:55
本文介绍了从派生类调用基本构造函数时不确定参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在游戏中实现继承时遇到了一些麻烦.我认为最好引入继承,因为它会使我的代码更整洁并以逻辑方式进行组织.

I am having some troubles implementing inheritence into my game. I thought that it would be best to introduce inheritence, as it would make my code neater and organised in a logical way.

从派生类的基类调用构造函数时,我面临的atm问题是参数.

The problem i am facing atm is with the parameters when calling the constructor from the base class in the derived class.

我将Paddle类用作基类,将Player类用作继承Paddle的类.

I am using the Paddle class as the base class and the Player class as the class inheriting Paddle.

代码如下所示:

在Paddle类中....

In the Paddle class....

//properties

  protected Texture2D pTexture;
  protected Vector2 pPosition;
  protected Vector2 pVelocity;


//constructor for Paddle class (only first line here, as the rest is irrelevant)

public Paddle(Texture2D newTexture, Vector2 newPosition, Viewport theViewport, Color newColour)

在Player类中....

In the Player class....

//properties
PlayerIndex pID;


//constructor for Player class (only first line here, as the rest is irrelevant)
public Player(PlayerIndex newID) : base()

在您提到它不起作用的原因之前,是因为我在Player类中调用的基本构造函数没有任何参数,而在基类/Paddle类中的构造函数却没有任何参数,我明白这一点.我面临的问题是设置播放器的纹理,位置等,但是纹理,位置等在基类中,因为它们是Player和Enemy类(从Paddle继承的另一个类,但类似)的常见属性给播放器,因此现在只提及它).我也不知道在Player类中调用基本构造函数时要把什么作为参数.

Before you mention the reason why its not working is because the base constructor im calling in the Player class doesn't have any parameters, whereas the constructor in the base class/Paddle class does, i understand that. The problem i am facing is setting the texture, position, etc of the player but the texture, position, etc are in the base class, as they are common properties for the Player and Enemy class (another class that inherits from Paddle but it's similar to Player, hence only mentioning it now). I also don't know what to put as the parameters when calling the base constructor in the Player class.

感谢您的阅读和预先的帮助:-)

Thanks for reading and for the help in advance :-)

我在下面更改了添加的代码:

ADDED CODE I'VE CHANGED BELOW:

public Player(PlayerIndex newID, Texture2D newTexture, Vector2 newPosition, Viewport theViewport, Color newColour)
        : base(newTexture, newPosition, theViewport, newColour)

推荐答案

要以这种方式使用构造函数,必须将基本构造函数所需的所有信息提供给派生的构造函数,以便它可以这样调用:

To use the constructor in this way, you have to give the derived constructor ALL of the information needed for the base constructor, so that it can call it like so:

public Player(PlayerIndex newID, Texture2D newTexture, Vector2 newPosition, Viewport theViewport, Color newColour)

然后您转到基地:

public Player(PlayerIndex newID, Texture2D newTexture, Vector2 newPosition, Viewport theViewport, Color newColour) : base(newTexture, newPosition, theViewport, newColour);

显然,这会使Player构造函数大很多.为避免此问题,您可以在基类(不可重写)上声明一个 Init()函数,该函数采用构造函数执行的所有参数,并在实例化后让拥有的类调用它.无论哪种方式,您最终都必须向班级提供所有信息.

Obviously this makes the Player constructor quite a bit larger. To avoid this problem, you could declare an Init() function on the base class (non-overridable) that takes all the parameters the constructor does, and have the owning class call it after instantiation. Either way, you have to give the class all the information eventually.

让我知道我是否可以澄清任何事情或进一步帮助!.

Let me know if I can clarify anything or help further!.

更新

  1. 是的,:base(...)语法实际上与编写(如果是有效的C#)语法相同. myBaseClassPointer = new base(...); (在这种情况下,基本为Paddle).同样,这不是有效的C#.使用非默认构造函数时,实际上必须使用:base实例化基类.它以与直接实例化属性相同的方式设置属性,并且只要不将它们标记为私有,就可以从派生类中使用它们(子类有点用词不当,因为该术语通常是指组合).

  1. Yes, the :base(...) syntax is effectively the same thing as writing (if it were valid C#) myBaseClassPointer = new base(...); (where base is Paddle in this case). Again, this is not valid C#. You actually have to use :base to instantiate the base class when using a non-default constructor. It sets properties the same way as if you had instantiated it directly, and as long as they are not marked private, they will be available from the derived class (child is a bit of misnomer, since that term generally refers to composition).

我不建议使用 Init()函数,因为不能保证using类可以调用它.话虽如此,它可以像这样很好地工作:

I'm not recommending the use of an Init() function, as the using class isn't guaranteed to call it. That being said, it could work pretty well like so:

class Player
{
   private bool initialized = false;
   public Player()
   {}

   public void Init(Texture2D newTexture, Vector2 newPosition, Viewport theViewport, Color newColour)
   {
      //All the stuff your constructor used to do
      initialized = true;
   }

   public void SomeFunctionUsingVariables()
   {
   if (initialized)
   { //Do whatever }
   }
}

唯一的优点是您不必将Init()所需的所有数据传递给Player构造函数,因此这些字段的知识不需要存在于Player类本身中.只要可以进行检查,并且您记得在实例化类中调用 Init ,它就可以工作.

The only advantage is you don't have to pass all the data that Init() needs to the Player constructor, so knowledge of these fields doesn't need to exist in the Player class itself. It works, as long as the extra if checks are ok and you remember to call Init in the instantiating class.

这篇关于从派生类调用基本构造函数时不确定参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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