0
一个问题。我经常看到那些东西:游戏游戏和继承
public Constructor(Game game, string effectAssetName)
: base(game)
我真的不能理解第二行的功能。这叫做基地但是为了什么? Game游戏的第一行中是否已经定义了游戏?
一个问题。我经常看到那些东西:游戏游戏和继承
public Constructor(Game game, string effectAssetName)
: base(game)
我真的不能理解第二行的功能。这叫做基地但是为了什么? Game游戏的第一行中是否已经定义了游戏?
“base”调用决定调用超类的构造函数 - 例如如果没有:base(游戏),超类将不会被初始化(确切的说,那个特定的构造函数将不会运行,但是如果有可用的构造函数,则可能是无参数的构造函数)
通常当你添加Game类你自己的功能,但你仍然需要Game类来初始化和实现它自己的功能。你基本上做以下电话
MyGameObject.Constructor(game, effectAssetName)
和
Game.Constructor(game);
而且(坏:))例如
class Fruit
{
private bool _hasPips;
public Fruit(bool hasPips)
{
_hasPips = hasPips;
}
}
class Apple : Fruit
{
private bool _isGreen;
public Apple(bool isGreen, bool hasPips) : base(hasPips)
{
_isGreen = isGreen;
}
}
当创建一个新的苹果,呼叫基地(hasPips)是在没有这个的情况下,Fruit超类的hasPips属性将永远不会被设置(实际上,在Apple上创建构造函数而不调用Fruit上的基本(布尔)构造函数是非法的,因为那里在Fruit上没有无参数的构造函数)