基类和派生类中的静态字段

发布于 2024-11-04 06:35:53 字数 169 浏览 0 评论 0原文

abstract基类中,如果我们有一些static字段,那么它们会发生什么?

它们的范围是从该基类继承的类还是仅继承它的类型(每个子类都有自己的来自 abstract 基类的 static 字段副本)?

In an abstract base class if we have some static fields then what happens to them ?

Is their scope the classes which inherit from this base class or just the type from which it is inheriting (each subclass has it's own copy of the static field from the abstract base class)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∝单色的世界 2024-11-11 06:35:53

static 成员完全特定于声明类;子类不会获得单独的副本。这里唯一的例外是泛型;如果开放泛型类型声明静态字段,则该字段特定于构成封闭泛型类型的类型参数的精确组合;即 Foo 将具有与 Foo 不同的静态字段,假设这些字段是在 Foo 上定义的。

static members are entirely specific to the declaring class; subclasses do not get separate copies. The only exception here is generics; if an open generic type declares static fields, the field is specific to that exact combination of type arguments that make up the closed generic type; i.e. Foo<int> would have separate static fields to Foo<string>, assuming the fields are defined on Foo<T>.

来日方长 2024-11-11 06:35:53

正如其他答案中所指出的,基类静态字段将在所有子类之间共享。如果您需要为每个最终子类提供单独的副本,则可以使用以子类名称作为键的静态字典:

class Base
{
    private static Dictionary<string, int> myStaticFieldDict = new Dictionary<string, int>();

    public int MyStaticField
    {
        get
        {
            return myStaticFieldDict.ContainsKey(this.GetType().Name)
                   ? myStaticFieldDict[this.GetType().Name]
                   : default(int);
        }

        set
        {
            myStaticFieldDict[this.GetType().Name] = value;
        }
    }

    void MyMethod()
    {
        MyStaticField = 42;
    }
}

As pointed out in other answer, the base class static field will be shared between all the subclasses. If you need a separate copy for each final subclass, you can use a static dictionary with a subclass name as a key:

class Base
{
    private static Dictionary<string, int> myStaticFieldDict = new Dictionary<string, int>();

    public int MyStaticField
    {
        get
        {
            return myStaticFieldDict.ContainsKey(this.GetType().Name)
                   ? myStaticFieldDict[this.GetType().Name]
                   : default(int);
        }

        set
        {
            myStaticFieldDict[this.GetType().Name] = value;
        }
    }

    void MyMethod()
    {
        MyStaticField = 42;
    }
}
谜兔 2024-11-11 06:35:53

以下代码说明静态成员的值在同一类型的抽象泛型的实例化之间共享。就像下面的示例一样,所有具有泛型类型“int”的实例化都将共享一个静态值,所有“string”都是另一个值。我最初认为它将在所有与泛型类型无关的实例化之间共享,但事实证明并非如此。

using System;
using System.Collections.Generic;
using System.Linq;

abstract class AbstractGeneric<T>
{
    private static int sharedCounter = 0;

    public AbstractGeneric()
    {
        sharedCounter++;
    }
    
    public int GetCounterValue()
    {
        return sharedCounter;
    }
}

class GenInt : AbstractGeneric<int> {}
class GenInt2 : AbstractGeneric<int> {}
class GenStr : AbstractGeneric<string> {}

public class Program
{
    public static void Main()
    {
        var instance1 = new GenInt();
        var instance2 = new GenStr();
        var instance3 = new GenInt2();
        var instance4 = new GenInt();

        Console.WriteLine(instance1.GetCounterValue()); // Output: 3
        Console.WriteLine(instance2.GetCounterValue()); // Output: 1
        Console.WriteLine(instance3.GetCounterValue()); // Output: 3
        Console.WriteLine(instance4.GetCounterValue()); // Output: 3
    }
}

The following code illustrates that the static member's value is shared among the instantiation of the abstract generic of the same type. Like in the example below all instantiations with generic type 'int' would share one static value, all 'string's another value. I was initially thinking that it would be shared among all instantiation agnostic to the generic type, but turned out that's not the case.

using System;
using System.Collections.Generic;
using System.Linq;

abstract class AbstractGeneric<T>
{
    private static int sharedCounter = 0;

    public AbstractGeneric()
    {
        sharedCounter++;
    }
    
    public int GetCounterValue()
    {
        return sharedCounter;
    }
}

class GenInt : AbstractGeneric<int> {}
class GenInt2 : AbstractGeneric<int> {}
class GenStr : AbstractGeneric<string> {}

public class Program
{
    public static void Main()
    {
        var instance1 = new GenInt();
        var instance2 = new GenStr();
        var instance3 = new GenInt2();
        var instance4 = new GenInt();

        Console.WriteLine(instance1.GetCounterValue()); // Output: 3
        Console.WriteLine(instance2.GetCounterValue()); // Output: 1
        Console.WriteLine(instance3.GetCounterValue()); // Output: 3
        Console.WriteLine(instance4.GetCounterValue()); // Output: 3
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文