通用类和静态字段 [英] Generic classes and static fields

查看:70
本文介绍了通用类和静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在几个不同的泛型类之间共享一个静态变量?

Is there a way to share one static variable between several different generic classes ?

我上课

class ClassA <T> : ObservableCollection<T> {

    static int counter;

    //...
}

及其带有不同参数实例化的几个实例,例如

and a couple of instances of it with different parameter instantiations, like

ClassA<int> a = new ClassA<int>();
ClassA<double> b = new ClassA<double>();
ClassA<float> c = new ClassA<float>();

有没有办法让实例a,b和c共享静态字段计数器?

Is there a way that the instances a, b, and c share the static field counter ?

任何答案和评论都非常感激:)

Any answers and comments are pretty much appreciated :)

推荐答案

您可以将计数器包装在自己的单例类中,然后从A,B和C引用计数器类.

You could wrap the counter in it's own singleton class, then reference the counter class from A, B, and C.

这篇关于通用类和静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆