简体   繁体   English

带成员的C#静态成员类

[英]C# static member class with members

I have some classes with a static member: 我有一些静态成员的类:

class BuildingA{
   static BuildableComponent buildableA;
}

class BuildingB{
   static BuildableComponent buildableB;
}

This static member has it's own members: 这个静态成员拥有自己的成员:

class BuildableComponent{
    int cost;
}

I would like to be able to manipulate the members of the static via the BuildingA and BuildingB classes, eg A.buildableA.cost and B.buildableB.cost - the way I've described it doesn't quite work, but is there a way to do this? 我希望能够通过BuildingA和BuildingB类来操纵静态成员,例如A.buildableA.cost和B.buildableB.cost - 我所描述的方式不太有用,但是有没有这样做的方法?

Fields are private by default in C# - you need to add a public access modifier to it: 默认情况下,字段在C#中是私有的 - 您需要向其添加public访问修饰符:

class BuildableComponent
{
    public int cost;
}

But, as recommended by @EamonnMcEvoy you can make it a property: 但是,根据@EamonnMcEvoy的建议,您可以将其设为属性:

class BuildableComponent
{
    public int Cost { get; private set; }
}

Properties are recommended because you can make the field readable from other classes, without allowing other classes to modify the property (as I have done above by making the setter private). 建议使用属性,因为您可以从其他类中读取字段,而不允许其他类修改属性(正如我上面通过将setter设为私有)。 They have other advantages, one being that they can be overridden in derived classes if required. 它们还有其他优点,一个是如果需要可以在派生类中重写它们。

In C# 6 you can also omit the setter entirely, forcing the value to be set only from the constructor and making the property immutable: 在C#6中,您还可以完全省略setter,强制仅从构造函数设置值并使属性不可变:

class BuildableComponent
{
    public BuildableComponent(int cost)
    {
        Cost = cost;
    }

    public int Cost { get; }
}

You have an additional problem in that the BuildableComponent fields inside BuildingA and BuildingB are static . 您还有一个问题,即BuildingABuildingB中的BuildableComponent字段是static This means that they belong to the class, and not an instance to the class (ie each instance of the class shares the same value). 这意味着它们属于类,而不是类的实例(即类的每个实例共享相同的值)。 This means you need to access it in via the class name rather than an instance of the class: 这意味着您需要通过类名而不是类的实例来访问它:

int cost = BuildingA.buildableA.cost;

In this particular case I would ask yourself whether you want this component to be static. 在这种特殊情况下,我会问自己是否希望这个组件是静态的。 If you are going to create multiple BuildingA instances, do you want them to share the same components? 如果要创建多个BuildingA实例,是否希望它们共享相同的组件? If not, make them non-static. 如果没有,请将它们设为非静态。

In c#, members on a class default to private , therefore cost can only be accessed from within an instance of BuildableComponent . 在c#中,类上的成员默认为private ,因此只能从BuildableComponent的实例中访问cost

You need to add the public access modifier to your cost field, or better yet, make it a property with a get and set: 您需要将public访问修饰符添加到cost字段中,或者更好的是,使其成为具有get和set的属性:

class BuildableComponent{
    public int cost;
}

OR 要么

class BuildableComponent{
    public int Cost { get; set; };
}

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

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