真实世界的抽象类和接口示例? [英] Real world examples of abstract classes and interfaces?

查看:71
本文介绍了真实世界的抽象类和接口示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关如何使用抽象类和接口的一些真实示例?

What are some real world examples of how to use abstract classes and interfaces?

推荐答案

抽象类是一个您无法创建实例的类。它可以提供基本功能,但是为了使用该功能,一个或多个其他类必须从抽象类派生。抽象类的一个主要好处是可以重用代码而无需重新键入代码。这有很多好处,例如减少错误和加快编码速度。抽象类的具体示例是名为Animal的类。你在现实生活中看到很多动物,但只有动物种类。也就是说,你永远不会看到紫色和毛茸茸的东西,并说这是一种动物,没有更具体的方法来定义它。相反,你会看到一只狗,一只猫或一只猪......所有的动物。关键是,你永远不会看到一只动物走来走去,而不是更具体的东西(鸭子,猪等)。 Animal是抽象类,Duck / Pig / Cat都是派生自该基类的类。动物可能会提供一种称为年龄的功能,为动物增加1年的生命。它还可能提供一个名为IsDead的抽象方法,当被调用时,它将告诉您动物是否已经死亡。由于IsDead是抽象的,每只动物都必须实现它。因此,一只猫可能会在它达到14岁后决定死亡,但鸭子可能会认为它在5岁之后死亡。抽象类Animal为从它派生的所有类提供Age函数,但每个类都必须自己实现IsDead。

An abstract class is a class that you cannot create an instance of. It can provide basic functionality, but in order for that functionality to be used, one or more other classes must derive from the abstract class. One of the major benefits of abstract classes is that you can reuse code without having to retype it. That has a plethora of benefits, such as reducing bugs and making coding faster. A concrete example of an abstract class would be a class called Animal. You see many animals in real life, but there are only kinds of animals. That is, you never look at something purple and furry and say "that is an animal and there is no more specific way of defining it". Instead, you see a dog or a cat or a pig... all animals. The point is, that you can never see an animal walking around that isn't more specifically something else (duck, pig, etc.). The Animal is the abstract class and Duck/Pig/Cat are all classes that derive from that base class. Animals might provide a function called "Age" that adds 1 year of life to the animals. It might also provide an abstract method called "IsDead" that, when called, will tell you if the animal has died. Since IsDead is abstract, each animal must implement it. So, a Cat might decide it is dead after it reaches 14 years of age, but a Duck might decide it dies after 5 years of age. The abstract class Animal provides the Age function to all classes that derive from it, but each of those classes has to implement IsDead on their own.

现在,接口就像一个抽象类,除了它不包含任何逻辑。相反,它指定了一个接口。因此,可能存在一个名为IFly的接口。这可能包含GoForward和GoDown方法。这些方法实际上不包含任何逻辑......实现IFly接口的每个类都必须实现那些GoForward和GoDown方法。您可以使用Duck和Finch类实现IFly接口。然后,如果要保留可以飞行的实例列表,只需创建一个包含IFly类型项的列表。这样,您可以将Ducks和Finches以及类的任何其他实例添加到列表中。

Now, an interface is like an abstract class, except it does not contain any logic. Rather, it specifies an interface. So, there might be an interface called IFly. This might have the methods GoForward and GoDown. Those methods would not actually contain any logic... each class that implements interface IFly would have to implement those GoForward and GoDown methods. You could have classes Duck and Finch implement interface IFly. Then, if you want to keep a list of instances that can fly, you just create a list that contains items of type IFly. That way, you can add Ducks and Finches and any other instance of a class the implements IFly to the list.

因此,抽象类可用于合并和共享功能虽然接口可用于指定将在不同实例之间共享的常用功能,而不实际为它们构建该功能。两者都可以帮助您以不同的方式缩小代码。接口和抽象类之间还有其他区别,但这些差异取决于编程语言,所以我不会在这里讨论其他差异。

So, abstract classes can be used to consolidate and share functionality, while interfaces can be used to specify what the common functionality that will be shared between different instances will be, without actually building that functionality for them. Both can help you make your code smaller, just in different ways. There are other differences between interfaces and abstract classes, but those depend on the programming language, so I won't go into those other differences here.


意味着现实世界,如包含 
抽象类或界面的实时软件系统或者你的意思是一个展示其有用性的人为举例?

Do you mean real-world as in "A live software system which includes  Abstract classes or interfaces" or do you mean "A contrived example which demonstrates their usefullness"?

如果你的意思是后者认为 Vehicle 作为抽象类。你不能对它做任何事情,因为你不知道它做了什么,或者如何驾驶它。

If you mean the latter think of Vehicle as an abstract class. You can't yet do anything with it because you have no idea what it does, or how to drive it.

abstract class Vehicle {}

车辆可以分为摩托车和踏板动力,但这仍然是抽象的,我们仍然不知道如何处理它。

Vehicles could be split into morotized and pedal-powered, but still this is abstract, we still dont know what to do with it.

抽象类MotorVehicle:Vehicle {}  

抽象类PedaledVehicle :Vehicle {}

您现在可以定义一个具体的(非抽象)类,如汽车。

You could now define a concrete (non-abstract) class, like car.

class MotorCar:MotorVehicle {}

  Intefaces派上用场,您只能从一个基类继承。所以想象一些车辆可以驾驶,其他车辆可以遥控,有些车辆使用方向盘,其他车辆不用

 Intefaces come in handy you can only inherit from one base class. So imagine some vehicles are drivable, others are remote controlled, some vehicles use a stearing wheel, others dont

界面IDrivable {}

interface IHasStearingWheel {}

现在你可以从它的基础派生出一个DrivableMotorCar,并实现其他行为。

Now you could derive a DrivableMotorCar from its base clas, and also implement other behaviours.

class DrivableMotorCar:MotorVehicle,IDrivable,IHasStearingWheel {}


请查看以下网站,我认为这会有所帮助。



http://shivasoft.in/blog/programming/java/difference-between-interfaceinheritance-abstract-class/ [ ^ ]
Please check below Site, i think it will help.

http://shivasoft.in/blog/programming/java/difference-between-interfaceinheritance-abstract-class/[^]


这篇关于真实世界的抽象类和接口示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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