简体   繁体   English

何时使用抽象类?

[英]When to use abstract classes?

Here is the MSDN article on abstract classes , but I really don't get it...这是关于抽象类的 MSDN 文章,但我真的不明白......

When should I really use abstract classes?我什么时候应该真正使用抽象类? What are the advantages of using abstract classes?使用抽象类有什么好处?

Abstract classes are useful when you need a class for the purpose of inheritance and polymorphism, but it makes no sense to instantiate the class itself, only its subclasses. 当您需要一个类来实现继承和多态时,抽象类很有用,但实例化类本身,只有它的子类是没有意义的。 They are commonly used when you want to define a template for a group of subclasses that share some common implementation code, but you also want to guarantee that the objects of the superclass cannot be created. 当您想要为一组共享一些常见实现代码的子类定义模板时,通常会使用它们,但您也希望保证不能创建超类的对象。

For instance, let's say you need to create Dog, Cat, Hamster and Fish objects. 例如,假设您需要创建Dog,Cat,Hamster和Fish对象。 They possess similar properties like color, size, and number of legs as well as behavior so you create an Animal superclass. 它们具有类似的属性,如颜色,大小和腿数以及行为,因此您可以创建一个Animal超类。 However, what color is an Animal? 但是,什么颜色的动物? How many legs does an Animal object have? Animal对象有多少条腿? In this case, it doesn't make much sense to instantiate an object of type Animal but rather only its subclasses. 在这种情况下,实例化Animal类型的对象而不仅仅是它的子类没有多大意义。

Abstract classes also have the added benefit in polymorphism–allowing you to use the (abstract) superclass's type as a method argument or a return type. 抽象类在多态中也有额外的好处 - 允许您使用(抽象)超类的类型作为方法参数或返回类型。 If for example you had a PetOwner class with a train() method you can define it as taking in an object of type Animal eg train(Animal a) as opposed to creating a method for every subtype of Animal. 例如,如果你有一个带有train()方法的PetOwner类,你可以将它定义为接受Animal类型的对象,例如train(Animal a),而不是为Animal的每个子类型创建一个方法。

By using abstract classes we are able to make the class more generic. 通过使用抽象类,我们可以使类更通用。

For example: if class A is an abstract class and there are classes class B, class C and class D extending abstract class A then these sub-classes will inherit a method which is already declared in abstract class A thereby making the method more generic. 例如:如果类A是一个抽象类,并且有类B类,类C和类D扩展抽象类A,那么这些子类将继承一个已经在抽象类A中声明的方法,从而使该方法更通用。

您将它们用于永远不会创建的类(因此实际上不存在),但是出于多态原因,您希望从它们继承它们。

Use abstract classes when you are defining behaviour for a class in your class heirarchy that is never going to be used to instantiate an object directly. 在为类heirarchy中的类定义行为时,请使用抽象类,该类永远不会用于直接实例化对象。

So, think of yourself as God for a moment. 所以,暂时把自己想象成上帝吧。 Your CBabyBoy and CBanyGirl classes wouldn't be abstract - as these are solid objects that do get created. 您的CBabyBoy和CBanyGirl类不是抽象的 - 因为这些是可以创建的实体对象。 On the other hand, your CPerson and CAnimal classes WOULD be abstract - they're useful from a type hierarchy point of view, but you won't ever be running CAnimal dingbat = new Animal(); 另一方面,你的CPerson和CAnimal类应该是抽象的 - 从类型层次结构的角度看它们是有用的,但是你永远不会运行CAnimal dingbat = new Animal();

Richard has provided an example were an abstract class has advantages over non-abstract classes. 理查德提供了一个例子 ,一个抽象类比非抽象类有优势。

I would like to add a fact-table for choosing between an abstract class and an interface. 我想添加一个事实表,用于在抽象类和接口之间进行选择。 The image can be found here . 图像可以在这里找到。

在此输入图像描述

Basically, you should use an abstract class, when some entity in your hierarchy logically will have method(s) it does not know how to implement, but it's descendants do. 基本上,你应该使用一个抽象类,当你的层次结构中的某个实体逻辑上将拥有它不知道如何实现的方法时,但它的后代却是如此。 There are billions of 'real life' examples all over the web, really) 网上有数十亿“现实生活”的例子,真的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM