什么时候使用抽象类?

发布于 2024-08-27 18:11:42 字数 155 浏览 7 评论 0原文

这是关于抽象类的MSDN文章,但我真的不知道明白了...

我什么时候应该真正使用抽象类?使用抽象类有什么好处?

Here is the MSDN article on abstract classes, but I really don't get it...

When should I really use abstract classes? What are the advantages of using abstract classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

初见你 2024-09-03 18:11:42

当您需要一个类用于继承和多态性时,抽象类很有用,但实例化类本身而仅实例化其子类是没有意义的。当您想要为一组共享一些公共实现代码的子类定义模板,但又想保证无法创建超类的对象时,通常会使用它们。

例如,假设您需要创建狗、猫、仓鼠和鱼对象。它们具有相似的属性,如颜色、大小、腿数以及行为,因此您可以创建一个 Animal 超类。然而,动物是什么颜色的? Animal 对象有多少条腿?在这种情况下,实例化 Animal 类型的对象没有多大意义,而只实例化其子类。

抽象类在多态性方面还具有额外的好处 - 允许您使用(抽象)超类的类型作为方法参数或返回类型。例如,如果您有一个带有 train() 方法的 PetOwner 类,您可以将其定义为接受 Animal 类型的对象,例如 train(Animal a),而不是为 Animal 的每个子类型创建一个方法。

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. They possess similar properties like color, size, and number of legs as well as behavior so you create an Animal superclass. However, what color is an Animal? How many legs does an Animal object have? In this case, it doesn't make much sense to instantiate an object of type Animal but rather only its subclasses.

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 e.g. train(Animal a) as opposed to creating a method for every subtype of Animal.

以可爱出名 2024-09-03 18:11:42

通过使用抽象类,我们可以使类更加通用。

例如:如果类A是一个抽象类,并且有类B、类C和类D扩展了抽象类A,那么这些子类将继承抽象类A中已经声明的方法,从而使该方法更加通用。

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.

何必那么矫情 2024-09-03 18:11:42

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

You use them for classes which will never be created (so effectively don't exist), but you want to inherit from them for polymorphism reasons.

埋葬我深情 2024-09-03 18:11:42

Richard 提供了一个例子,抽象类比非抽象类具有优势抽象类。

我想添加一个事实表来在抽象类和接口之间进行选择。该图片可以在此处找到。

输入图片此处描述

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.

enter image description here

淡莣 2024-09-03 18:11:42

当您为类层次结构中永远不会用于直接实例化对象的类定义行为时,请使用抽象类。

所以,暂时把自己想象成上帝。您的 CBabyBoy 和 CBanyGirl 类不会是抽象的 - 因为这些是确实被创建的实体对象。另一方面,您的 CPerson 和 CAnimal 类将是抽象的 - 从类型层次结构的角度来看它们很有用,但您永远不会运行 CAnimal dingbat = new Animal();

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.

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. 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();

我乃一代侩神 2024-09-03 18:11:42

基本上,您应该使用抽象类,当层次结构中的某些实体逻辑上具有它不知道如何实现的方法时,但它的后代知道如何实现。网络上有数十亿个“现实生活”的例子,真的)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文