静态类为什么被认为是“类”和“引用类型”? [英] Why are static classes considered “classes” and “reference types”?

查看:127
本文介绍了静态类为什么被认为是“类”和“引用类型”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天一直在思考关于C#和CIL类型系统,我已经开始想知道为什么静态类被认为是类。有许多方法中,他们并不是真正的类:

I’ve been pondering about the C# and CIL type system today and I’ve started to wonder why static classes are considered classes. There are many ways in which they are not really classes:


  • 正常类可以包含非静态成员,静态类可以'T。在这方面,一类是比较类似的结构的比它是一个静态类,但结构有一个单独的名称。

  • 您可以有一个提及正常类的实例,而不是一个静态类(尽管它是被认为是引用类型)。在这方面,一类是更类似于一个的接口的比它是一个静态类,但接口都有一个单独的名称。

  • 的名称静态类不能在任何地方使用,其中一个类型名称通常适合:你不能声明此类型的变量,则不能使用它作为一个基本类型,你不能把它作为一个泛型类型参数。在这方面,静态类是有点更像的命名空间

  • 正常类可以实现接口。再次,这使得班更类似于结构的而不是静态类。

  • 正常类可以从另一个类继承。

  • A "normal" class can contain non-static members, a static class can’t. In this respect, a class is more similar to a struct than it is to a static class, and yet structs have a separate name.
  • You can have a reference to an instance of a "normal" class, but not a static class (despite it being considered a "reference type"). In this respect, a class is more similar to an interface than it is to a static class, and yet interfaces have a separate name.
  • The name of a static class can never be used in any place where a type name would normally fit: you can’t declare a variable of this type, you can’t use it as a base type, and you can’t use it as a generic type parameter. In this respect, static classes are somewhat more like namespaces.
  • A "normal" class can implement interfaces. Once again, that makes classes more similar to structs than to static classes.
  • A "normal" class can inherit from another class.

这也是奇怪的是静态类被认为是从的 System.Object的的派生。虽然这使他们能够继承的静态方法等于的ReferenceEquals 的,即继承的目的是值得商榷的,你会呼吁的对象反正。 C#中甚至可以让你明确指定的静态类无用继承,但不能在接口上或结构,其中来自的对象的隐式的推导和的 System.ValueType 的分别,实际上有。目的

It is also bizarre that static classes are considered to derive from System.Object. Although this allows them to "inherit" the static methods Equals and ReferenceEquals, the purpose of that inheritance is questionable as you would call those methods on object anyway. C# even allows you to specify that useless inheritance explicitly on static classes, but not on interfaces or structs, where the implicit derivation from object and System.ValueType, respectively, actually has a purpose.

关于子集的特性参数:静态类有类的功能的子集,但他们也有一个的结构的特征子集。所有这一切使一个类从其他类型的类型的不同的事情,似乎并不适用于静态类。

Regarding the subset-of-features argument: Static classes have a subset of the features of classes, but they also have a subset of the features of structs. All of the things that make a class distinct from the other kinds of type, do not seem to apply to static classes.

关于 typeof运算的说法:制作一个静态类到一个新的和不同类型的类型不从的的typeof

,不应该他们已经制作成一个单独的一种类型,而不是一个特殊的类的?

Given the sheer oddity of static classes, and the scarcity of similarities between them and "normal" classes, shouldn’t they have been made into a separate kind of type instead of a special kind of class?

推荐答案

是的,他们很奇怪。他们确实有一些类类似的行为,如能够有(静态)成员变量,并限制访问使用公共/私有成员。

Yes, they are very odd. They do have some class-like behavior, like being able to have (static) member variables, and restricting access to members using public/private.

我几乎键入公共/保护/私有有,但显然保护没有意义,因为静态类的都没法继承。我认为,这种情况的主要原因是,因为没有的情况下,你不能有多态性,但是这是不是真的继承的唯一原因。多态性是伟大的,但有时你只是想借用大部分基类的功能,并添加自己的几件事情。正因为如此,有时你会看到静态类切换到使用单模式,只是这样,它可以从基本的类集合利用某些功能。在我看来这是一个哈克企图缩小这个差距,而且它变得混乱,并引入了大量的非自然的复杂性。另一种选择是聚集,那里的孩子类方法只是通过对父类方法调用传递,但是这是需要大量的代码来施蒂希它一起,是不是一个真正的完美的解决方案无论是。

I almost typed "public/protected/private" there, but obviously protected doesn't make sense, because there is no method inheritance of static classes. I think the main reason for this is that because there are no instances, you can't have polymorphism, but that is not really the only reason for inheritance. Polymorphism is great, but sometimes you just want to borrow most of the functionality of the base class and add a few things of your own. Because of this, sometimes you'll see static classes switched to use singleton patterns, just so that it can leverage the some functions from base set of classes. In my opinion this is a hacky attempt to close that gap, and it gets confusing and introduces a lot of unnatural complexity. The other option is aggregation, where the child class methods just pass calls through to the parent class methods, but this is requires a lot of code to stich it all together and isn't really a perfect solution either.

这些天,静态类通常只是用作全局方法的替换,即,方法,只是提供的功能不被绑定到任何东西的实例。二OO纯粹主义者所厌恶的免费/全球任何漂浮的任何概念,但你也不想要有一个不必要的实例和对象左右浮动,如果你只是需要的功能,所以静态的类提供了一个中间地面妥协,双方可以排序的认同。

These days, static classes are usually just used as a replacement for global methods, i.e. methods that just provide functionality without being bound to an instance of anything. The OO purists hate any concept of a free/global anything floating around, but you also don't want to have to have an unnecessary instance and object floating around if you just need functionality, so a static "class" provides a middle-ground compromise that both sides can sort of agree with.

所以,是的,静态类是怪异。理想情况下,这将是很好,如果他们可以被分解为自己的概念,只要你从不需要绑定到一个实例方法(我们用静态类现在得到的,使用放心,灵活轻便),以及组合这些方法到容器中(我们也有现在),而且还提供了定义从中将继承的方法(这是现在缺少的部分)基础实体的​​能力。此外,这将是伟大它是从班一个单独的概念,整整你提高的原因,它只是变得混乱,因为人们自然期望类与特性,可以创建和销毁方法的实例。

So yes, static classes are weird. Ideally, it would be nice if they could be broken into their own concept that provided the flexibility and lightweight ease-of-use that you get from methods that don't need to be bound to an instance (which we have now with static classes), and also group those methods into containers (which we also have now), but also provide the ability to define a base entity from which it will inherit methods (this is the part that is missing now). Also, it would be great it was a seperate concept from classes, for exactly the reasons you raise, it just gets confusing because people naturally expect classes to be instances with properties and methods that can be created and destroyed.

这篇关于静态类为什么被认为是“类”和“引用类型”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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