标题应该清楚,我无法访问具有多重继承的最低级别属性。
对象A扩展了对象B. 对象B扩展了对象C
对象C有一个我想从对象A访问的属性,但我只能出于某种原因从对象B访问它。同时适用于变量和函数。
我正在使用自定义libbrary - “Windows游戏库(4.0)”。不使用库时,我从来没有遇到任何问题。现在和唯一的区别在于我现在在库中的类上使用“public”关键字,因为否则会出现“无法访问”的错误。
在代码中:
namespace ExampleGame
{
class Player : Actor
{
public Player()
{
//most things happen in gameobject<actor<this
MaxSpeed = new Vector2(10, 10);
Acceleration = new Vector2(5, 5);
Velocity = new Vector2(0, 0);
MaxJumpPower = 15;
}
override public void Update()
{
base.Update();
manageInput();
}
}
}
namespace GridEngineLibrary.objects
{
public class Actor : GameObject
{
public int MaxJumpPower;
public Actor()
{
canMove = true;
}
/// <summary>
/// moves the object but it's acceleration
/// </summary>
public void jump()
{
if (grounded == true)
{
Console.WriteLine("jump!");
Direction.Y = -1;
Velocity.Y = MaxJumpPower * -1;
}
}
}
}
namespace GridEngineLibrary.objects
{
public class GameObject
{
public Vector2 location;
public SpriteBatch spritebatch;
public Vector2 hitArea;
public AnimatedTexture graphic;
public Vector2 Velocity;
public Vector2 Acceleration;
public Vector2 MaxSpeed;
public Vector2 Direction;
public int Z = 1;
public bool canMove;
public GameObject()
{
spritebatch = SpriteManager.spriteBatch;
}
/// <summary>
/// set the animated texture
/// </summary>
/// <param name="location"></param>
/// <param name="size"></param>
/// <param name="TextureName">name of texture to load</param>
public void setAnimatedTexture(Vector2 location, Vector2 size, string TextureName,
int totalFrames, int totalStates, int animationSpeed = 8,
int spacing = 9)
{
graphic = new AnimatedTexture(location, size, TextureName);
graphic.isAnimated = true;
graphic.totalStates = totalStates;
graphic.totalFrames = totalFrames;
graphic.animationSpeed = animationSpeed;
graphic.spacing = spacing;
hitArea = size;
}
virtual public void Update()
{
graphic.update(location);
}
}
}
答案 0 :(得分:0)
这绝不是专家的答案,但您可以随时在B类中公开C类所需的属性,从而从A类的基础访问它们。
我确信有一个更优雅的解决方案,但只是一种镜像(或者它可能被称为隧道?),通过B的属性至少应该是可用的。
答案 1 :(得分:0)
Actor
命名空间中可能有一个ExampleGame
类,或者某个名称空间由Player继承的using指令导入。这与Actor
命名空间中的GridEngine.objects
类不同。