C# 抽象类的具体使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/239127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exact use of Abstract class
提问by
What is the exact use of an Abstract class? Is not possible to do the same things in an ordinary class as it is an an abstract class?
Abstract 类的确切用途是什么?不可能在普通类中做与抽象类相同的事情吗?
回答by AdamC
An abstract class is used when you have some base functionality that you want subclasses to inherit, but it wouldn't make sense to instantiate the base class. For example, if you had something like a Shape base class, you could have some built in implementation that could be used by subclasses as well as interface methods that you want the subclasses to implement. However, it probably wouldn't make sense to create a Shape object. An abstract class gives you this functionality. Another great example of abstract class uses is the abstract factory pattern.
当您希望子类继承某些基本功能时,使用抽象类,但实例化基类没有意义。例如,如果您有像 Shape 基类这样的东西,您可以有一些内置实现,可以由子类使用,以及您希望子类实现的接口方法。但是,创建 Shape 对象可能没有意义。抽象类为您提供了此功能。抽象类使用的另一个很好的例子是抽象工厂模式。
回答by Corbin March
Use an abstract class to provide some concrete implementation but not allow instantiation. You can always instantiate an ordinary class which doesn't make sense if it can't stand alone. At the same time, an interface might not be enough if there's a concrete implementation that's identical in all implementing classes. An abstract class is just enough.
使用抽象类来提供一些具体的实现,但不允许实例化。你总是可以实例化一个普通的类,如果它不能独立存在就没有意义。同时,如果有一个在所有实现类中都相同的具体实现,那么一个接口可能还不够。一个抽象类就足够了。
- Interface: contract only, no implementation, no instantiation
- Abstract class: contract, some implementation, no instantiation
- Class: contract, implementation, instantiation
- 接口:只有合约,没有实现,没有实例化
- 抽象类:契约,一些实现,没有实例化
- 类:契约、实现、实例化
回答by Jay Bazuzi
Unlike regular classes, abstract
classes can contain abstract
methods. They act much like interface members.
与常规类不同,abstract
类可以包含abstract
方法。他们的行为很像界面成员。
Meanwhile, they can do just about everything else that regular classes can do: they can implement methods, contain fields & nested types, derive from another class, etc.
同时,它们几乎可以做普通类可以做的所有其他事情:它们可以实现方法、包含字段和嵌套类型、从另一个类派生等等。
回答by user31768
Regular classes require you to provide implementations for all methods.
Interfaces require you to notprovide any implementations for all methods.
常规类要求您提供所有方法的实现。
接口要求您不要为所有方法提供任何实现。
Abstract classes are the only type of class that allow you to both have methods that do contain an implementation, and have methods that do not provide an implementation, but require an inheriting class to provide one.
抽象类是唯一一种允许您拥有包含实现的方法和不提供实现但需要继承类提供实现的方法的类。
The fact that you are allowed to add methods without an implementation is the reason you cannot instantiate an abstract class: you can only instantiate something that has implementations for all its methods.
允许在没有实现的情况下添加方法的事实是您不能实例化抽象类的原因:您只能实例化具有其所有方法实现的东西。