封装和抽象之间的确切区别是什么?
当前回答
抽象:抽象的意思是显示功能的哪一部分。
封装:封装意味着隐藏功能的How部分。
让我们举一个非常简单的例子
/// <summary>
/// We have an Employee class having two properties EmployeeName and EmployeeCode
/// </summary>
public class Employee
{
public string EmplpyeeName { get; set; }
public string EmployeeCode { get; set; }
// Add new employee to DB is the main functionality, so are making it public so that we can expose it to external environment
// This is ABSTRACTION
public void AddEmployee(Employee obj)
{
// "Creation of DB connection" and "To check if employee exists" are internal details which we have hide from external environment
// You can see that these methods are private, external environment just need "What" part only
CreateDBConnection();
CheckIfEmployeeExists();
}
// ENCAPLUSATION using private keyword
private bool CheckIfEmployeeExists()
{
// Here we can validate if the employee already exists
return true;
}
// ENCAPLUSATION using private keyword
private void CreateDBConnection()
{
// Create DB connection code
}
}
控制台应用程序程序类
class Program
{
static void Main(string[] args)
{
Employee obj = new Employee();
obj.EmplpyeeName = "001";
obj.EmployeeCode = "Raj";
// We have exposed only what part of the functionality
obj.AddEmployee(obj);
}
}
其他回答
简而言之
抽象使用->封装 & 封装使用->数据隐藏
OR
数据隐藏是封装和的子集 封装是抽象的一个子集
参考:http://www.tonymarston.co.uk/php-mysql/abstraction.txt
抽象是广义的术语。即封装是抽象的子集。
Abstraction | Encapsulation |
---|---|
It solves an issue at the design level. | Encapsulation solves an issue at implementation level. |
hides the unnecessary detail but shows the essential information. | It hides the code and data into a single entity or unit so that the data can be protected from the outside world. |
Focuses on the external lookout. | Focuses on internal working. |
Lets focus on what an object does instead of how it does it. | Lets focus on how an object does something. |
Example: Outer look of mobile, like it has a display screen and buttons. | Example: Inner details of mobile, how button and display screen connect with each other using circuits. |
示例:解决方案架构师是创建整个解决方案的高级抽象技术设计的人,然后将该设计移交给开发团队进行实现。 在这里,解决方案架构师充当抽象,而开发团队充当封装。
举例:用户数据的封装(组网)
图片由
Abstraction (or modularity) – Types enable programmers to think at a higher level than the bit or byte, not bothering with low-level implementation. For example, programmers can begin to think of a string as a set of character values instead of as a mere array of bytes. Higher still, types enable programmers to think about and express interfaces between two of any-sized subsystems. This enables more levels of localization so that the definitions required for interoperability of the subsystems remain consistent when those two subsystems communicate. Source
Java示例
封装隐藏了实现细节,这些细节可能是通用的,也可能不是专门的行为。
抽象提供了一种泛化(例如,在一组行为之上)。
这里有一个很好的阅读:抽象、封装和信息隐藏,作者是Object Agency的Edward V. Berard。
我读得越多,就越困惑。所以,我的理解是:
封装:
我们通常从外面看到手表,它的组件被封装在它的身体里。我们对不同的操作有某种控制。这种隐藏细节和公开控制(例如设置时间)的方式就是封装。
抽象:
到目前为止,我们一直在谈论手表。但我们没有具体说明是哪种手表。可以是数字的,也可以是模拟的,可以是手用的,也可以是墙用的。有很多可能性。我们所知道的是,这是一块手表,它显示时间,这是我们唯一感兴趣的东西,时间。这种隐藏细节和公开通用特性或用例的方法就是抽象。
许多答案和例子都具有误导性。
封装是将“数据”和“对该数据进行操作的函数”打包到单个组件中,并限制对某些对象组件的访问。 封装意味着对象的内部表示通常隐藏在对象定义之外的视图中。
抽象是一种表示基本特性而不包括实现细节的机制。
封装:——信息隐藏。 抽象:——实现隐藏。
示例(c++):
class foo{
private:
int a, b;
public:
foo(int x=0, int y=0): a(x), b(y) {}
int add(){
return a+b;
}
}
foo类的任何对象的内部表示都隐藏在该类的外部。——>封装。 foo对象的任何可访问成员(data/function)都是受限的,只能由该对象访问。
foo foo_obj(3, 4);
int sum = foo_obj.add();
方法add的实现是隐藏的。——>抽象。