简体   繁体   English

调用在编译时未指定的方法

[英]Calling a method that is unspecified at compile time

Tinkering in Unity, I have a part of my GUI that will render information (eg enemy stats) from one of several different class types representing different things in the game. 在Unity中修修补补,我的GUI的一部分将呈现代表游戏中不同事物的几种不同类类型之一的信息(例如,敌人统计数据)。 All of the different classes have a method for creating an information object that contains the desired information (eg enemyInfoObject), which have a DrawInfo() method that draws the info depending on the gameObject. 所有不同的类都有一个用于创建包含所需信息的信息对象的方法(例如,敌方信息对象),该方法具有一个DrawInfo()方法,该方法根据gameObject绘制信息。

In my information-rendering script I would like to be able to assign any one of the different information objects to a single variable (ie the currently selected enemy, NPC, etc.) and be able to call the DrawInfo() method. 在我的信息呈现脚本中,我希望能够将任何一个不同的信息对象分配给一个变量(即当前选择的敌人,NPC等),并能够调用DrawInfo()方法。 Is there a clean, simple, and/or better way to do this? 有没有一种干净,简单和/或更好的方法来做到这一点?

I can elaborate if needed. 如果需要,我可以详细说明。

This is a well-known design pattern called the "Strategy Pattern" . 这是一种众所周知的设计模式,称为“策略模式” It extends the idea of "programming to an interface". 它扩展了“编程到接口”的思想。

The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. 该策略模式旨在提供一种定义一系列算法的方法,将每个算法封装为一个对象,并使它们可互换。 The strategy pattern lets the algorithms vary independently from clients that use them. 策略模式使算法可以独立于使用它们的客户端而变化。

First, define an interface containing DrawInfo ,say ISprite . 首先,定义一个包含DrawInfo的接口,例如ISprite All your sprites will be classes that implement this interface. 您所有的精灵都是实现此接口的类。 Now the game can store a reference to the selected ISprite and call the method on it. 现在,游戏可以存储对所选ISprite的引用,并在其上调用方法。

For example: 例如:

public interface ISprite
{
    void Draw();
}

public class BossMonster : ISprite
{
    public override void Draw()
    {
        // Draw scary stuff
    }
}

public class NPC : ISprite
{
    public override void Draw()
    {
        // Draw stuff
    }
}

public class Game
{
    private ISprite currentSprite = ...
    private List<ISprite> otherSprites = ...

    public void Render()
    {
        currentSprite.Draw();

        foreach (ISprite sprite in otherSprites)
        {
            sprite.Draw();
        }
    }
}

The best way I can think of is to make all of the objects in your game that you'd like to call DrawInfo() on implement an interface like IDrawableInfo : 我能想到的最好的方法是在实现IDrawableInfo类的接口上制作要调用DrawInfo()的游戏中的所有对象:

IDrawableInfo.cs IDrawableInfo.cs

public interface IDrawableInfo
{
    void DrawInfo();
}

EnemyInfoObject.cs EnemyInfoObject.cs

public class EnemyInfoObject : IDrawableInfo
{
    public void DrawInfo()
    {
        // Do drawing stuff here
    }
}

YourScript.cs YourScript.cs

if(objectIdLikeToDrawInfoFor is IDrawableInfo)
{
    objectIdLikeToDrawInfoFor.DrawInfo();
}

You can avoid runtime errors by wrapping your DrawInfo() calls in an if statement like this. 通过将DrawInfo()调用包装在这样的if语句中,可以避免运行时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM