gpt4 book ai didi

C# 调用基础构造函数

转载 作者:太空宇宙 更新时间:2023-11-03 18:31:21 24 4
gpt4 key购买 nike

我用 Google 搜索“调用基础构造函数”,但没有得到我需要的答案。

这是我的构造函数;

public class defaultObject
{
Vector2 position;
float rotation;
Texture2D texture;

public defaultObject(Vector2 nPos, float nRotation, Texture2D nTexture)
{
position = nPos;
rotation = nRotation;
texture = nTexture;
}
}

现在我已经准备就绪,我想继承构造函数及其所有工作原理。这是我希望做的;

public class Block : defaultObject
{
// variables inherited from defaultObject
public Block : defaultObject; //calls defaultObject constructor
}

为什么我不能那样做?

最佳答案

使用:base():

public class Block : defaultObject
{
// variables inherited from defaultObject
public Block ()
: base()
{}
}

或带参数:

public class Block : defaultObject
{
// variables inherited from defaultObject
public Block (Vector2 nPos, float nRotation, Texture2D nTexture)
: base(nPos, nRotation, nTexture)
{}
}

关于C# 调用基础构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21907527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com