封装和抽象之间的确切区别是什么?


当前回答

抽象是对用户隐藏无用的数据 封装是将数据绑定到一个胶囊(类)中。 我认为封装是我们实现抽象的方式。

其他回答

抽象是广义的术语。即封装是抽象的子集。

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示例

抽象或封装并不严格要求信息隐藏。信息可能会被忽略,但不一定要隐藏。

封装是一种将事物视为单一事物的能力,即使它可能由许多复杂的部分或思想组成。例如,我可以说我坐在一把“椅子”上,而不是指这把椅子上的许多不同部分,每个部分都有特定的设计和功能,它们精确地组合在一起,目的是让我的屁股舒适地离地板几英尺远。

Abstraction is enabled by encapsulation. Because we encapsulate objects, we can think about them as things which relate to each other in some way rather than getting bogged down in the subtle details of internal object structure. Abstraction is the ability to consider the bigger picture, removed from concern over little details. The root of the word is abstract as in the summary that appears at the top of a scholarly paper, not abstract as in a class which can only be instantiated as a derived subclass.

I can honestly say that when I plop my butt down in my chair, I never think about how the structure of that chair will catch and hold my weight. It's a decent enough chair that I don't have to worry about those details. So I can turn my attention toward my computer. And again, I don't think about the component parts of my computer. I'm just looking at a part of a webpage that represents a text area that I can type in, and I'm communicating in words, barely even thinking about how my fingers always find the right letters so quickly on the keyboard, and how the connection is ultimately made between tapping these keys and posting to this forum. This is the great power of abstraction. Because the lower levels of the system can be trusted to work with consistency and precision, we have attention to spare for greater work.

抽象:抽象的意思是显示功能的哪一部分。

封装:封装意味着隐藏功能的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);
    }
}

通过使用单个通用示例进行抽象和封装

------------------------------------------------------------------------------------------------------------------------------------

我们都用计算器来计算复杂的问题!

封装意味着隐藏数据,比如使用getter和setter等。

抽象意味着-隐藏实现使用抽象类和接口等。