请你解释一下抽象类和接口的实时例子 [英] can u please explain real time example of abstract class and interface

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

问题描述

请你解释一下抽象类和接口的实时例子。我知道它们之间的区别但是我仍然不明白抽象类在哪种情况下使用以及在哪种情况下使用接口

can u please explain real time example of abstract class and interface . I know the difference between them but Still I am not understand in which situation abstract class is use and in which situation interface is used

推荐答案

抽象类允许您派生一个具体类,它继承了一组核心功能:与真实单词中的汽车大致相同,是一个抽象对象,但福特福特嘉年华允许您开车去商店。 福特嘉年华级别继承了所有汽车共同的一系列期望:它将拥有四个车轮,一个发动机,以及一个让驾驶员坐下来控制它的速度和方向的地方。但你不能走进经销商并购买汽车 - 你必须去那里期待获得特定品牌和型号的汽车:福特和嘉年华,并从那里决定发动机的尺寸和类型,一个修剪级别,等等。



界面是不同的:它不允许你继承任何现有的行为,但强制你必须做一些事情正确的方式 - 它是合同而不是基类。在现实世界中,将界面视为俱乐部的会员资格:如果您按时支付会费,并遵循俱乐部规则,您仍然可以成为会员并获得此类会员的好处。但是没有什么可以阻止你同时加入第二个俱乐部 - 只要你也能遵守其规则并缴纳会费。
An abstract class allows you to derive a "concrete" class which inherits a set of core functionality: in much the same way as "a Car" in the real word is an abstract object, but "a Ford Fiesta" allows you to drive to the shops. The "Ford Fiesta" class inherits a set of expectations that are common to all Cars: it will have four wheels, an engine, and a place for a driver to sit and control it's speed and direction. But you can't walk into a dealership and buy "a car" - you have to go there expecting to get a specific make and model of car: "Ford" and "Fiesta" and from there decide on an engine size and type, a trim level, and so forth.

An Interface is different: it doesn't allow you to inherit any existing behaviours but enforces that you must do some things in the right way - it's a contract rather than a base class. In the real world, think of an Interface as the membership of a club: provided you pay your dues on time, and follow the club rules you can remain a member and get the benefits of such membership. But there is nothing stopping you from joining a second club at the same time - provided you can also obey its rules, and pay its dues.


接口是一个类签名。它不包含任何功能,也不能保存值。它用于确保实现接口的类具有签名方法和/或属性,然后可以通过调用函数调用它们。



Abstract类可以包含功能,但是像接口一样无法实例化。你可以拥有不需要的静态方法和实例,或者你可以从中继承,知道核心功能是可用的。



这是基本的。谷歌上有更多细节和许多例子
An Interface is a class signature. It does not contain any functionality and cannot hold values. It is used to ensure that classes implementing the interface have the signature methods and/or properties, which can then be called by a calling function.

An Abstract class can contain functionality but, like an interface cannot be instantiated. You can have static methods that don'r require and instance or you can inherit from it knowing that the core functionality is available.

That's the basic. There is much more detail and many examples on google


这篇关于请你解释一下抽象类和接口的实时例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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