【问题标题】:xna multilevel inheritance - cannot access base-base class propertiesxna 多级继承 - 无法访问基类属性
【发布时间】:2011-08-25 23:02:39
【问题描述】:

标题应该很清楚,我在使用多重继承访问最低级别的属性时遇到问题。

对象 A 扩展对象 B 对象 B 扩展对象 C

对象 C 有一个我想从对象 A 访问的属性,但由于某种原因我只能从对象 B 访问它。适用于变量和函数。

我正在使用自定义库 - 确切地说是“Windows 游戏库 (4.0)”。不使用图书馆时,我从来没有遇到过任何问题。与现在唯一的区别是我现在在库中的类上使用“public”关键字,否则我会收到“无法访问”的错误。

在代码中:

对象 A

    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();
    }

}
}

对象 B

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;
        }
    }

}
}

对象 C

    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);
    }
}

}

【问题讨论】:

  • 我刚刚将您的课程放入 LINQPad 中,并且我能够从 Player 访问 GameObject 的成员,前提是他们至少具有 protected 访问权限。这就是我所期望的。您可以从Actor 访问GameObject 成员,但不能从Player 访问是没有意义的。我认为您的类驻留在不同的项目中一定存在构建问题,但没有复制我只是猜测的错误。
  • grounded 定义在哪里?如果它应该在 GameObject 类中,并且您编译的代码,我建议您以某种方式从不同的 GameObject 类继承。您可能只是在此处的示例代码中遗漏了某些内容,但我想我会提出来。

标签: c# inheritance xna multi-level


【解决方案1】:

这绝不是专家级的答案,但您总是可以从 Class B 中的 Class C 中公开您想要的属性,从而从 Class A 中的 base 中访问它们。

我确信有一个更优雅的解决方案,但只是某种镜像(或者可能称为隧道?)通过 B 的属性至少应该是可用的。

【讨论】:

  • 这确实是可能的,但让一切变得如此混乱:(
【解决方案2】:

ExampleGame 命名空间中可能有一个 Actor 类,或者由 Player 继承自的 using 指令导入的某个命名空间。这与 GridEngine.objects 命名空间中的 Actor 类不同。

【讨论】:

  • ExampleGame中没有Actor类,ExampleGame只有“Game”类(应用程序访问点)和Player类。
猜你喜欢
  • 2013-09-27
  • 2019-10-21
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 2014-06-19
  • 2017-09-01
  • 2013-11-05
相关资源
最近更新 更多