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

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

问题描述

可能的重复:
抽象类和静态有什么区别一个?

你好
我想知道 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 和 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.

相比之下,您根本无法创建静态类的实例.

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天全站免登陆