【问题标题】:Access methods in class contained in another class without referencing the contained class访问包含在另一个类中的类中的方法,而不引用包含的类
【发布时间】:2020-10-04 16:05:04
【问题描述】:

我有一个 Screen 类,其中包含来自 Monogame 的 SpriteBatch。我想访问SpriteBatch.Draw()方法,而不必做Screen.spriteBatch.Draw(),而是:

screen.Draw(sprite, position, color);

我可以通过定义这样的方法来做到这一点:

class Screen
{
    public void Draw(Texture2D texture, Rectangle rect, Color color)
    {
        spriteBatch.Draw(texture, rect, color);
    }
    public void Draw(Texture2D texture, Vector2 position, Color color)
    {
        spriteBatch.Draw(texture, position, color);
    }

    .... and so on for every other override of SpriteBatch.Draw
}

但这显然很长​​,我宁愿不必手动输入每个覆盖。

有没有更简单的方法来做到这一点?

【问题讨论】:

  • 您确定您已在正确的级别抽象了功能吗?您已经为屏幕提供了 spritebatch,通常这意味着您希望屏幕负责使用 sprintbatch 来绘制一些东西——比如场景、复杂的动作等——而不仅仅是表达包含的类使用的相同功能。

标签: c# class monogame


【解决方案1】:

让 Screen 类继承自 SpriteBatch,代码如下:

Screen : SpriteBatch

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多