抽象与抽象类 [英] abstraction vs abstract class

查看:84
本文介绍了抽象与抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://www.cs.cornell.edu /courses/cs211/2006sp/Lectures/L08-abstraction/08_abstraction.html

抽象有两种形式.一种是函数抽象,另一种是数据抽象.但是抽象类适合什么地方?据我所知,抽象类是一个完全不同的概念,即使该名称表明它与OOP原理有关.

abstraction comes in two flavors. One is function abstraction and the other is data abstraction. But where do abstract classes fit in? As far as i see, abstract classes are a totally different concept and even though the name suggests that it has something to do with OOP principles.

有人可以对此有所说明吗?

Could someone please shed some light on this ?

推荐答案

这些是非常不同的概念.

These are very different concepts.

抽象类似于黑匣子的概念.输入进来,黑匣子执行某些操作,输出出来.黑匣子中发生的事情无关紧要,您只需要知道它就可以了. Java的散列函数就是一个真实的例子,用户只需知道它就对输入值进行散列,对用户来说,如何对数字进行散列并不重要.黑匣子是抽象的.关键是您不必知道它是如何工作的,只要知道它的工作原理即可.

Abstraction is similar to the concept of a black box. Input goes in, black box does something, output comes out. It doesn't matter what happens in the black box, all you have to know is that it works. A real life example of this is java's hash function, all the user has to know is that it hashes the input value, it doesn't matter to the user how the number gets hashed. The black box is the abstraction. The point is you don't have to know how it works, just that it does.

抽象类(至少在Java中是)是接口和完整的OOP类之间的混合体.接口定义了任何扩展类都必须具有的方法,并在代码中约定了将正确实现该接口并确保一切都能按预期运行的代码.抽象类具有这些空方法(协议),并且具有可以调用的完全实现的方法.

Abstract classes (at least in Java) are a mixture between interfaces and full OOP classes. An interface defines methods that any extending class must have, its an agreement in the code that it will implement the interface properly and assures everything will work as expected. An abstract class has these empty methods(agreements) and also has fully implemented methods that can be called.

这篇关于抽象与抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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