简体   繁体   English

您可以更改创建的用户的默认值 class c#

[英]Can you change the default value for a user created class c#

I'm making a game and the background is made of Tile objects.我正在制作游戏,背景由 Tile 对象组成。 The Tile parent is abstract but I have Tile arrays to hold the children. Tile parent 是抽象的,但我有 Tile arrays 来容纳孩子。 When I make a Tile array I want it to assume that any unspecified object inside a Tile array is a BlankTile(x, y) where x and y is the position in the array.当我创建一个 Tile 数组时,我希望它假定 Tile 数组中任何未指定的 object 都是 BlankTile(x, y),其中 x 和 y 是数组中的 position。

abstract class Tile 
{
   Point position;
   public Tile(int x, int y){position = new Point(x, y);}
}

public class BlankTile : Tile
{
   public BlankTile(int x, int y) : base(x, y)
    {position = new Point(x, y);}
}
Tile[] tiles = new Tile[5];

foreach(Tile aTile in tiles)
{
   Console.WriteLine(aTile.GetType().ToString());
}
//output:
//BlankTile
//BlankTile
//BlankTile
//BlankTile
//BlankTile

(if this is impossible or the incorrect way to go about this or a loop is just simpler please let me know and please tell me about a better way) (如果这是不可能的,或者关于这个或循环更简单的 go 的不正确方法,请告诉我,请告诉我更好的方法)

Good question, but per C# design the default value for a class is null .好问题,但根据 C# 设计,class 的默认值为null

When you initialize an array of any class type, like new Tile[10] it is like assigning default(Tile) to each element.当您初始化任何 class 类型的数组时,例如new Tile[10] ,它就像将default(Tile)分配给每个元素。 The deault keyword returns null for classes (including string ), 0 for intrinsic numeric types and enum and the default constructor for structs. deault关键字为类(包括string )返回null ,为内部数字类型和enum返回0 ,为结构返回默认构造函数。

But you can create an array filled with a specific value using Enumerable.Repeat(item,count)但是您可以使用Enumerable.Repeat(item,count)创建一个填充特定值的数组

public abstract class Tile
{
    public static readonly BlankTile Origin = new BlankTile(0, 0);

    protected Tile(Point position)
    {
        Position = position;
    }

    public Point Position { get; }
}

public class BlankTile : Tile
{
    public BlankTile(int x, int y)
        : base(new Point(x, y))
    {  }        
}

class Program
{
    static void Main(string[] args)
    {
        Tile[] array = Enumerable.Repeat<Tile>(Tile.Origin, 100).ToArray();
    }
}

There are many ways you can achieve this:您可以通过多种方式实现这一目标:

  • You can treat null values as BlankTile s in code.您可以在代码null值视为BlankTile This would save you some unnecessary initialization, but you need to do that everywhere, which isn't very convenient, and is also prone to errors.这会为您节省一些不必要的初始化,但您需要在任何地方都这样做,这不是很方便,而且也容易出错。

  • You can create a Tiles class and change the initialization of the encompassed array in the constructor.您可以创建一个Tiles class 并在构造函数中更改包含数组的初始化。 You can provide an indexer property to emulate array access syntax if you'd like.如果您愿意,可以提供索引器属性来模拟数组访问语法。

  • You can create a helper function Tile[] CreateTileArray(int numTiles) instead of using new [] syntax, which can also initialize the members with BlankTile s.您可以创建一个帮助程序 function Tile[] CreateTileArray(int numTiles)而不是使用new []语法,这也可以使用BlankTile s 初始化成员。 You need to remember to call this helper function whenever you create a Tile array though.不过,无论何时创建 Tile 数组,都需要记住调用此助手 function。

  • Instead of multiple classes, you can use a single struct with a TileType enum, and that could be initialized to TileType.Blank as the default.您可以使用带有TileType枚举的单个结构,而不是多个类,并且可以将其初始化为TileType.Blank作为默认值。 Writing code handling this kind of layout usually involves many checks, so may not be as performant as separate classes with virtual methods depending on how many types of tiles you'll have.编写处理这种布局的代码通常涉及许多检查,因此可能不如使用虚拟方法的单独类那样高效,具体取决于您将拥有多少类型的图块。

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

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