C#中的抽象类与静态类 [英] Abstract classes vs Static classes in C#

查看:141
本文介绍了C#中的抽象类与静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

抽象类和静态类之间有什么区别?

Hello

我想知道C#中抽象类和静态类之间的所有区别是什么?使用什么以及为什么?

Hello
I Would like to know what are all the differences between abstract classes and static classes in C#
When do I use what and why?

抽象类是真的我们不能创建其实例的类吗?

谢谢

Is it true the abstract class is a class which we cannot create instances of it?
Thanks

推荐答案


我想知道C#中抽象类和静态类之间的所有区别是什么。

I would like to know what are all the differences between abstract classes and static classes in C#.

不要问这样的问题。我可以花几个小时列出数百个差异,这些差异与您无关。

Don't ask questions like that. I could spend hours listing hundreds of differences, none of which would be relevant to you.


C#中抽象类和静态类之间最重要的区别是什么?

What is the most important difference between abstract classes and static classes in C#?

更像它。

抽象类通常用于在类型层次结构中建模。例如,卡车是一种车辆,而飞机是一种车辆,因此您可能有一个基本类Vehicle和派生类Truck and Airplane。但是车辆是抽象的。没有车辆,仅仅是没有某种特定事物的车辆。您可以用抽象类来表示该概念。

An abstract class is usually intended to model something in a type hierarchy. For example, a truck is a kind of vehicle, and an airplane is a kind of vehicle, so you might have a base class Vehicle and derived classes Truck and Airplane. But "Vehicle" is abstract; there are no vehicles which are just vehicles without being some more specific kind of thing. You represent that concept with an abstract class.

与之相反,静态类根本不打算为任何模型建模。这只是存储大量代码的一种便捷方法。真的,它根本不应该是一门课; VB通过将此类事物称为模块而不是类来做出更好的选择。尽管从技术上讲它们从对象继承,但是静态类在逻辑上根本不在类型层次结构中。它们只是容纳静态成员的一个存储桶。

A static class by contrast is not intended to model anything at all. It's just a convenient way of storing a bunch of code. Really it shouldn't be a class at all; VB made a better choice by calling such things "modules" rather than "classes". Though technically they inherit from object, static classes are logically not really in a type hierarchy at all. They're just a bucket for holding static members.

静态类通常用作扩展方法的容器。

Static classes are often used as containers of extension methods.


何时我使用什么以及为什么?

When do I use what and why?

当您要构建以下形式的模型时,请使用抽象类: X是一种Y。就像汽车是一种车辆或正方形是一种形状或杂志是一种出版物一样,其中 Y是一个抽象概念。不要将其用于员工是一种人之类的事情-人应该具体。人不是一个抽象的概念。有的人就是人,但没有没有其他的车辆。

Use an abstract class when you want to build a model of the form "an X is a kind of Y". Like "a Car is a kind of Vehicle" or "a Square is a kind of Shape" or "a Magazine is a kind of Publication", where the "Y" is an abstract concept. Don't use it for things like "an Employee is a kind of Person" -- Person should be concrete. Person is not an abstract concept; there are people who are just people, but there are no vehicles that are not something else.

当您要创建扩展方法或拥有扩展方法时,请使用静态类一堆在逻辑上适合在一起但不与任何对象关联的代码。例如,如果您有一堆相关的数学例程,那么这是静态课程的理想选择。

Use a static class when you want to make extension methods, or when you have a bunch of code that fits logically together but does not associate with any object. For example, if you have a bunch of related math routines, that's a good candidate for a static class.


抽象类是否是我们无法创建其实例的类?

Is it true the abstract class is a class which we cannot create instances of it?

否。那是不是。您可以创建抽象类的实例。可以通过创建更多派生类的实例来实现。

No. That is not true. You can create instances of an abstract class. You do so by creating an instance of a more derived class.

Vehicle v = new Car();

显然,v是Vehicle的一个实例,因此您可以创建一个抽象类的实例。您不能做的是创建一个抽象类的实例,该实例又不是一个派生的具体类的实例。

Clearly v refers to an instance of Vehicle, and therefore you can create an instance of an abstract class. What you cannot do is create an instance of an abstract class that is not also an instance of a more derived concrete class.

相反,您不能创建a的实例。

By contrast, you cannot create an instance of a static class at all.

这是您没有问过的问题:

Here's a question you didn't ask:


静态类和抽象类之间的实现关系是什么?

What is the implementation relationship between static classes and abstract classes?

静态类实际上并不是CLR中的概念。当您在类上说静态时,我们实际上所做的是生成一个没有公共构造函数的抽象密封类。由于它是抽象的,因此您不能直接创建一个。由于它是密封的,因此您无法创建更多派生的类并将其实例化。

Static classes actually do not really exist as a concept in the CLR. When you say "static" on a class, what we actually do is generate an abstract sealed class with no public constructors. Since it is abstract, you cannot create one directly. Since it is sealed, you cannot create a more derived class and instantiate that.

这篇关于C#中的抽象类与静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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