How to add different types of objects in a single array in C#?

前端 未结 10 1987
情话喂你
情话喂你 2020-12-28 14:17

I am planning to rewrite my Python Tile Engine in C#. It uses a list of all the game objects and renders them on the screen. My problem is that unlike in Python where you ca

10条回答
  •  佛祖请我去吃肉
    2020-12-28 14:22

    You can write an abstract base class called GameObject, and make all gameObject Inherit it.

    Edit:

    public abstract class GameObject
    {
            public GameObject();
    }
    public class TileStuff : GameObject
    {
        public TileStuff()
        {
    
        }
    }
    public class MoreTileStuff : GameObject
    {
        public MoreTileStuff()
        {
    
        }
    }
    public class Game
    {
        static void Main(string[] args)
        {
            GameObject[] arr = new GameObject[2];
            arr[0] = new TileStuff();
            arr[1] = new MoreTileStuff();
        }
    }
    

提交回复
热议问题