封装与抽象之间的区别 [英] Difference between Encapsulation and Abstraction

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

问题描述

我今天接受了采访.我有一个来自 OOP 的问题,关于封装与& 抽象?

I had an interview today. I had a question from OOP, about the difference between Encapsulation & Abstraction?

我回答了我,我了解到封装基本上是用来绑定数据成员的.成员函数合并为一个称为 Class 的单元.而抽象基本上是为了隐藏实现的复杂性.提供访问用户的便利.我以为她可以接受我的回答.但是她询问,如果两者的目的都是隐藏信息,那么两者之间的实际区别是什么?我无法给她任何答案.

I replied her to my knowledge that Encapsulation is basically to bind data members & member functions into a single unit called Class. Whereas Abstraction is basically to hide complexity of implementation & provide ease of access to the users. I thought she would be fine with my answer. But she queried, if the purpose of both are to hide information then what is the actual difference between these two? I could not give any answer to her.

在问这个问题之前,我阅读了StackOverFlow上有关这两个 OOP 概念之间的区别的其他主题.但是我并没有说服自己说服面试官.

Before asking this question, I read other threads on StackOverFlow about the difference between these two OOPs concepts. But I am not finding my self in a position to convince the interviewer.

任何人都可以用最简单的例子来证明它的合理性吗?

Can anyone please justify it with a simplest example?

推荐答案

封装隐藏变量或某些实现,这些实现可能经常在类中更改,以防止外部人访问直接.他们必须通过getter和setter方法访问它.

Encapsulate hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.

抽象也用于隐藏某些东西,但要更高的程度(类,界面).使用抽象类(或接口)的客户不必关心它是谁或是谁,他们只需要知道它可以做什么即可.

Abstraction is used to hiding something too but in a higher degree(class, interface). Clients use an abstract class(or interface) do not care about who or which it was, they just need to know what it can do.

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

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