封装和抽象之间的确切区别是什么?
当前回答
下面这段话帮助我理解了它们之间的区别:
数据封装是一种捆绑数据的机制 使用它们的函数和数据抽象是一种机制 只暴露接口,隐藏实现细节 来自用户。
你可以在这里阅读更多。
其他回答
封装:对对象的实际用户隐藏不需要的/不期望的/适当的实现细节。 如。
List<string> list = new List<string>();
list.Sort(); /* Here, which sorting algorithm is used and hows its
implemented is not useful to the user who wants to perform sort, that's
why its hidden from the user of list. */
抽象:是一种提供泛化的方法,因此是处理大量不同对象的通用方法。如。
class Aeroplane : IFlyable, IFuelable, IMachine
{ // Aeroplane's Design says:
// Aeroplane is a flying object
// Aeroplane can be fueled
// Aeroplane is a Machine
}
// But the code related to Pilot, or Driver of Aeroplane is not bothered
// about Machine or Fuel. Hence,
// pilot code:
IFlyable flyingObj = new Aeroplane();
flyingObj.Fly();
// fighter Pilot related code
IFlyable flyingObj2 = new FighterAeroplane();
flyingObj2.Fly();
// UFO related code
IFlyable ufoObj = new UFO();
ufoObj.Fly();
// **All the 3 Above codes are genaralized using IFlyable,
// Interface Abstraction**
// Fly related code knows how to fly, irrespective of the type of
// flying object they are.
// Similarly, Fuel related code:
// Fueling an Aeroplane
IFuelable fuelableObj = new Aeroplane();
fuelableObj.FillFuel();
// Fueling a Car
IFuelable fuelableObj2 = new Car(); // class Car : IFuelable { }
fuelableObj2.FillFuel();
// ** Fueling code does not need know what kind of vehicle it is, so far
// as it can Fill Fuel**
封装隐藏了实现细节,这些细节可能是通用的,也可能不是专门的行为。
抽象提供了一种泛化(例如,在一组行为之上)。
这里有一个很好的阅读:抽象、封装和信息隐藏,作者是Object Agency的Edward V. Berard。
抽象:抽象的意思是显示功能的哪一部分。
封装:封装意味着隐藏功能的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);
}
}
我试图在抽象和封装之间画一条线,根据我的观点,抽象更多的是概念性的东西,而封装是抽象实现的一种。因为一个人可以隐藏数据而不封装,例如使用私有常数或变量;所以我们可以用数据隐藏进行封装,但数据隐藏并不总是封装。在下面这段代码中,我试图描述这些概念的最简单形式。
// Abstraction
interface IOperation
{
int SquareNumber();
}
public class Operation
{
// Data hiding
private int number;
public Operation(int _number)
{
this.number = _number;
}
// Encapsulation
public int SquareNumber()
{
return number * number;
}
}
在行动,
IOperation obj = new Operation(2);
// obj.number <--- can't access because hidden from world using private access modifier but not encapsulated.
obj.SquareNumber(); // cannot access internal logic to calculate square because logic is hidden using encapsulation.
抽象是对用户隐藏无用的数据 封装是将数据绑定到一个胶囊(类)中。 我认为封装是我们实现抽象的方式。
推荐文章
- 什么是依赖倒置原则?为什么它很重要?
- 为什么在Python方法中需要显式地有“self”参数?
- c#中朋友的对等物是什么?
- 如何在方法中访问“静态”类变量?
- 为什么c#不提供c++风格的'friend'关键字?
- String, StringBuffer和StringBuilder
- 存储库和服务层的区别?
- DDD -实体不能直接访问存储库的规则
- 为什么STL如此严重地基于模板而不是继承?
- 如何在Objective-C中声明类级属性?
- 面向方面编程与面向对象编程
- c++中类似于java的instanceof
- 在python中遍历对象属性
- 将类代码分离为头文件和cpp文件
- 在PHP中使用getter和setter而不是函数或简单的公共字段有什么优点?