问题描述
学习 OOP 概念,特别有兴趣深入了解抽象和封装.
Learning OOP concepts especially interested to understand Abstraction and Encapsulation in depth.
已经检查了以下内容
如果没有一个真实而简单的示例类/代码片段,我很难理解这些概念.
I found very hard to understand those concepts with out a real and simple example class/code snippet.
我的一个同事说抽象就是创造抽象用作用域保护其成员变量的类和普通类是称为封装.
有没有一种简单的方法可以让我理解并帮助他人理解它们究竟是什么,而不是重复以下内容?
Is there a simple way I can understand and help others to understand what exactly they are, rather than repeating the below?
抽象和封装是互补的概念:抽象关注对象的可观察行为...封装专注于导致这种行为的实现......封装通常是通过信息隐藏来实现的,即是隐藏对象的所有秘密的过程有助于其基本特征.
推荐答案
抽象 是一个仅向用户显示相关"数据并隐藏"对象的不必要细节的过程.考虑一下您的手机,您只需要知道按下哪些按钮来发送消息或拨打电话,按下按钮时会发生什么,您的消息如何发送,您的电话如何连接都从用户.
Abstraction is a process where you show only "relevant" data and "hide" unnecessary details of an object from the user. Consider your mobile phone, you just need to know what buttons are to be pressed to send a message or make a call, What happens when you press a button, how your messages are sent, how your calls are connected is all abstracted away from the user.
封装是将数据和函数组合成一个称为类的单元的过程.在Encapsulation中,不直接访问数据;它是通过类中存在的函数访问的.简而言之,类的属性保持私有,并提供公共 getter 和 setter 方法来操作这些属性.因此,封装使数据隐藏的概念成为可能.
Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.
这篇关于理解封装和抽象的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!