我正在为我的游戏制作渐变动画,我得到了一个名为LoadContent的类,然后我写道:
public override void LoadContent (ContentManager Content, Texture2D image, string text, Vector2 position)
{
base.LoadContent(Content, image, text, position);
increase = false;
fadeSpeed = 1.0f;
defaultTime = new TimeSpan(0, 0, 1);
timer = defaultTime;
activateValue = 0.0f;
stopUpdating = false;
defaultAlpha = alpha;
}
但我遇到了一个错误,我试图删除覆盖,但它没有帮助,并试图将公共更改为私有,只是想看看它是否有效,但它不起作用。这就是错误:
'Xnaplatformer.FadAnimation.LoadContent(Microsoft.Xna.Framework.Content.ContentManager,Microsoft.Xna.Framework.Graphics.Texture2D,字符串,Microsoft.Xna.Framework.Vector2)":无法重写继承的成员'Xnaplatformer.Animation.LoadContent(Microsoft.Xna.Framework.Content.ContentManager,Microsoft.Xna.Framework.Graphics.Texture2D,字符串,Microsoft.Xna.Framework.VVector2)',因为它未标记为虚拟,抽象,或覆盖C:\Users\Mike\documents\visual studio2013\Projects\Xnaplatformer\Xnaplatormer\Xnaplatformer\FadeAnimation.cs Xnaplatform
首先,代码snippt中的LoadContent不是一个类。这是一种方法。当你询问LoadContent方法时,你会认为你试图在自定义游戏类中覆盖Game.LoadContent。将其标记为受保护的覆盖,游戏中的couse LoadContent也受到保护。
我希望这个例子能帮助你理解它是如何工作的:
class A
{
public virtual void DoSome1() // can be overriden couse marked as virtual
{
// logic for class A
}
public void DoSome2(); // should be overriden couse it abstract method that have no body by default; any abstartc method makes class abstract too
public void DoSome3() // can not be overriden: 1. not abstract; 2. not virtual
{
// logic for class A
}
protected virtual void DoSome4() // protected stil can be overriden; this method visible only inside class A and B
{
// logic for class A
}
private virtual void DoSome5() // virtual modifier is invalid couse private stil can not be overriden; visible only inside class A
{
// logic for class A
}
}
class B : A
{
public override void DoSome1() // use override keyword to re-define method from base class
{
// logic for class B
}
public override void DoSome2() // as A.DoSome2() is abstract you should override it B, or B also became abstratct
{
// logic for class B
}
new public void DoSome3() // this method not override A.DoSome3(), this is totally new method
{
// logic for class B
}
protected void DoSome4() // override A.DoSome4(); note: it also marked as protected
{
// logic for class B
}
}