封装概念 [英] Encapsulation concept

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

问题描述

我对封装的概念和实现有疑问。

I have problem with concept and implementation of encapsulation.

有人可以向我解释吗?

推荐答案

封装是一个中等程度的简单概念,一旦您意识到它(可能)与胶囊来自相同的基本词即可。

Encapsulation is a moderately easy concept once you realise it (probably) comes from the same base word as capsule.

信息包含。

封装意味着一个类仅发布其他人使用它所需要的内容,而不再发布。这就是所谓的信息隐藏,这意味着类可以完全改变其内部结构而不会影响任何用户。

Encapsulation means that a class publishes only what is needed for others to use it, and no more. This is called information hiding and it means classes can totally change their internals without having an effect on any of their users.

换句话说,字典类可以像一个简单的数组,然后发展为单词的二叉树,甚至可能发展为某些数据库访问函数,而所有这些都无需更改其接口。

In other words, a dictionary class can begin life as a simple array and progress to a binary tree of words then even maybe to some database access functions, all without changing the interface to it.

在面向对象的世界中,对象既保存其数据,又保存用于处理数据的方法,这就是封装的顶峰。完成此操作的一种方法是确保每个对象都知道要调用哪个函数来操纵其数据,并确保调用了正确的函数。

In an object oriented world, objects hold both their data and the methods used to manipulate data and that is the pinnacle of encapsulation. One way this is done is to make sure each object knows which functions to call to manipulate its data, and ensure the correct ones are called.

例如,这里是用于维护神话中的整数列表的类,但很奇怪,它是Python语言,因此希望很容易理解,语言:

As an example, here's a class for maintaining integer lists in my mythical, but strangely Pythonic and therefore hopefully easy to understand, language:

class intlist:
    private int val[10]
    private bool used[10]
    public constructor:
        for i in 0..9:
            used[i] = false

    public function add (int v):
        for i in 0..9:
            if not used[i]:
                used[i] = true
                val[i] = v
                return
        throw intlist-full

    public function del (int v):
        for i in 0..9:
            if used[i] and val[i] == v:
                used[i] = false
                return
        throw intlist-invalid-value

现在这里发布的信息是构造函数和两个用于添加和删除的函数。

Now the only information published here are the constructor and two functions for adding and deleting.

由于所有其他内容均已封装,因此可以随意更改不用破坏使用它的代码。

Because everything else is encapsulated, I can change it at will without breaking the code that uses it.

我可以使数组更长,可以将它们排序存储或存储在二进制树中,而不是存储在数组中以使其更快。只要发布的API不变,我就可以自由地做自己想做的事情。实际上,我还可以在不破坏其他代码的情况下向API添加内容,只是不能删除或更改它们所依赖的内容。

I could make the arrays longer, I could store them sorted or in a binary tree instead of an array to make it faster. As long as the published API doesn't change, I am free to do what I want. In fact, I can also add things to the API without breaking other code, I just can't delete or change anything they rely on.

您应该注意,封装并不是面向对象的新事物。在C语言中,通过确保信息隐藏在模块(通常是带有私有标头的源文件或其中的文件)中已经存在了很长时间。

You should note that encapsulation isn't something new with object orientation. It's been around for ages, in C by ensuring that information is hidden within a module (usually a source file or group thereof with private headers).

实际上, stdio.h FILE * 就是一个很好的例子。您不必在意指针到底在做什么,因为所有使用指针的函数都知道如何做这些事情。

In fact, the stdio.h FILE* stuff is a good example of this. You don't care what's actually behind the pointer since all the functions which use it know how to do their stuff.

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

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