简体   繁体   English

返回类实例的静态属性

[英]static property which returns class instance

I would like to use something similar as link ! 我想使用类似链接的东西! Part of the source code looks like that: 部分源代码如下所示:

public struct Vector3
{
    public static Vector3 zero { get; }
}

This function returns an already initialized Vector (which 0 on every axis). 该函数返回一个已经初始化的向量(每个轴上为0)。 Is this possible, because it is a struct? 这是可能的,因为它是结构? That's what I have: 那就是我所拥有的:

public class WorldBlock
{
    public enum e_WorldBlockType
    {
        Undef,
        Earth,
        Stone,
        Air // no block set
    }

    public static WorldBlock Empty { get; }
    public e_WorldBlockType BlockType { get; set; }
    public bool IsWalkable { get; set; }

    public WorldBlock(e_WorldBlockType blockType = e_WorldBlockType.Undef, bool isWalkable = false)
    {
        this.BlockType = blockType;
        this.IsWalkable = isWalkable;
    }
}

But I get this: error CS0840: 'WorldBlock.Empty.get' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors 但是我明白了: error CS0840: 'WorldBlock.Empty.get' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors error CS0840: 'WorldBlock.Empty.get' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors

How should I write the body? 我应该如何写尸体?

EDIT: If I add for example a private set , it compiles, but if I use it, I get a null pointer exception. 编辑:如果添加例如一个private set ,它会编译,但是如果我使用它,则会得到一个空指针异常。

Something like this: 像这样:

private static WorldBlock __empty = ...;

public static WorldBlock Empty {
  get {
    return WorldBlock __empty;
  }
}

If you only want an empty WorldBlock I suggest you write: 如果您只想要一个空的WorldBlock,建议您写:

public static readonly WorldBlock Empty = new WorldBlock();

Instead of your Property. 而不是您的财产。 However it's kind of dodgy since the setters of the "Empty" object is public, so there's really no guarantee that it's actually empty. 但是,由于“ Empty”对象的设置者是公共的,因此这有点儿狡猾,因此实际上不能保证它实际上是空的。

You have to do: 你必须做:

public static WorldBlock Empty
{
  get{return someThing; }
}

Replace someThing with whatever you were wanting Empty to store.... If thats null or whatever. 更换someThing ,不管你是想Empty存储....如果那null或什么的。

From your title... it looks like you want to return an instance of the class. 从您的标题...看来您想返回该类的实例。 If you only ever want one instance of this class... you're looking for the Singleton design pattern. 如果您只想要此类的一个实例,那么您正在寻找Singleton设计模式。

Should your "Empty" instances be shared and be the same? 您的“空”实例是否应该共享并且相同? If so, the following body is probably what you are looking for. 如果是这样,则下面的正文可能是您要寻找的。

private static WorldBlock emptyInstance;
public static WorldBlock Empty
{
   get
   {
      if (emptyInstance == null)
      {
         //initialize it
      }
      return emptyInstance;
   }
}

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

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