为什么不重写而不是使用抽象类? [英] Why not override instead of using abstract class?

查看:24
本文介绍了为什么不重写而不是使用抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对很多人来说可能是一个简单的问题,但让我感到困惑.我从 Kathy Sierra 中挑选了一个例子来展示抽象类的实用性,但我无法理解抽象类的整体重要性.

This might be a simple question for many but has confused me. I am picking an example from Kathy Sierra that shows the utility of Abstract Classes but I am unable to understand the overall importance of abstract classes.

示例我们有一个带有抽象方法的抽象类 Car - power() &topSpeed().这些方法在子类 BMWVolkswagenAudi 中实现.

Example We have an abstract class Car with abstract methods - power() & topSpeed(). These methods are implemented in sub classes BMW, Volkswagen and Audi.

我的问题是 - 为什么我们首先需要抽象类 Car 来为每种汽车类型自定义方法?为什么不在这些汽车子类型中的任何一个中使用这两种方法,比如宝马,然后其他两个 - 大众汽车和奥迪 - 可以简单地覆盖这些方法?

My question is - why do we need to have the abstract class Car in the first place to customize methods for each car type? Why not have these two methods in any one of these car subtypes, say BMW and then other two - Volkswagen and Audi - can simply override these methods?

推荐答案

通过使方法抽象,意味着人们必须实现它.你要求人们这样做,人们不可能忘记这样做,因为如果他们这样做,它就会编译失败.

By making a method abstract, it means that people have to implement it. You require people to do so and it is impossible for people to forget to do so, as it will fail to compile if they do.

@override 注释存在的原因非常相似,通过将方法标记为 @override,如果(例如)您输入了错误的方法名称,则会出现错误并且实际上并没有覆盖某些东西.

The @override annotation exists for a very similar reason, by marking a method as @override you get an error if (for example) you typed the method name wrong and aren't actually overriding something.

在许多方面,抽象类介于接口和普通类之间——它定义了您需要做什么才能以与接口相同的方式使用它,但它还为您处理一些实现.

In many ways the abstract class is half way between an interface and a normal class - it defines what you need to do to use it in the same way an interface does, but it also handles some of the implementation for you.

类只能扩展一个其他类.它们可以实现任意数量的接口.

Classes can only extend one other class. They can implement any number of interfaces.

例如,您可能让 CarMotorbikeTrain 继承了 MotorVehicle - 但随后您可能有Steerable 接口由 CarMotorbikePedalbike 实现.

For example you might have MotorVehicle inherited by Car, Motorbike and Train - but then you might have a Steerable interface implemented by Car, Motorbike and Pedalbike.

回答评论中的问题:

如果有一个接口I"有方法m()由类A"实现,另一个类B"想要访问方法m(),那么这里需要什么接口.我们可以简单地不在类 A 中实现该方法吗?

If there is an Interface "I" having method m() which is implemented by class "A" and another class "B" wants to access the method m(), what is the need of interface here. Can we simply not implement that method in class A?

你可以 - 但如果另一方面 B 类想要访问 m() 类中的方法 A 和 classC(其中 A 和 C 不相互继承或不继承包含 m() 的公共类)那么这样做的方法是指定一个公共接口 I 和类 B 使用接口类型,我,根本就不是 A 型和 C 型.

You can - but if on the other hand class B wants to access the method m() in both a class A and class C (where A and C don't inherit from each other or a common class containing m()) then the way to do that is to specify a common interface I and class B uses the interface type, I, not the types A and C at all.

还要记住接口可以在包和库之间使用.例如,ListenerStrategy 模式大量使用接口.当 Java 开发人员编写 JButton(例如)时,ActionLstener 被指定为一个接口,以便为将来使用 JButtons 的人们提供最大的灵活性.

Also remember that interfaces can be used between packages and libraries. For example Listener and Strategy patterns make heavy use of interfaces. When the Java developers wrote JButton (for example) the ActionLstener is specified as an Interface to provide maximum flexibility to people using JButtons in the future.

这篇关于为什么不重写而不是使用抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆