When and Why to use abstract classes/methods?
When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.) Use an abstract class to define a common base class for a family of types. Use an abstract class to provide default behavior. Subclass only a base class in a hierarchy to which the class logically belongs.
我没看懂上面引用的解释。请解释为什么要使用抽象类来创建类库?
最佳答案
你应该非常仔细地阅读这类文章。据我所知,文章的主要部分是专门介绍 c# 语言的。就这种语言而言,接口(interface)和抽象类之间存在很大差异。例如,c# 语言中的接口(interface)只是一组“纯”虚方法(它们不能在接口(interface)内定义,只有类才能实现它)。接口(interface)不能有构造函数。抽象类可以有构造函数。此外,c# 不支持多重继承(与 c++ 语言相反)。这样,C# 接口(interface)和抽象类看起来与 C++ 的接口(interface)和抽象类非常不同
关于c++ - 为什么要使用抽象类来创建类库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42970147/