封装和抽象的区别 [英] Difference between Encapsulation and Abstraction

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

问题描述

我今天接受了采访.我有一个来自 OOP 的问题,关于 EncapsulationEncapsulation 之间的区别抽象?

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 方法访问它.

Encapsulation 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 hide something too, but in a higher degree (class, interface). Clients who use an abstract class (or interface) do not care about what it was, they just need to know what it can do.

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

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