有哪些不同类型的封装?

时间:2022-09-02 09:36:05

What are the different types of encapsulation?

有哪些不同类型的封装?

Am I right in thinking this basically refers to central OO concepts such as Abstraction, Polymorphism and Inheritance?

我是否认为这基本上是指中心OO概念,如抽象,多态和继承?

My understanding of encapsulation is that it is a method of hiding data / functionality, but I never really considered Polymorphism or Inheritance a form of encapsulation, although I can see how polymorphism could be considered encapsulation as it can hide the exact type of the object you are interacting with.

我对封装的理解是它是一种隐藏数据/功能的方法,但我从来没有真正考虑过多态或继承是一种封装形式,虽然我可以看到多态如何被认为是封装,因为它可以隐藏对象的确切类型你与...互动。

So, would you say that's about it, or am I missing some core concepts?

那么,你会说是关于它的,还是我错过了一些核心概念?

edit I just noticed in the comments someone mentioned it could refer to private / public methods, perhaps I'm thinking in to the question too much and expecting a more complicated answer than it really is?

编辑我刚刚在评论中注意到有人提到它可以引用私人/公共方法,也许我正在考虑这个问题并且期待一个比实际更复杂的答案?

7 个解决方案

#1


You're thinking too much I think.

你想的太多了。

http://en.wikipedia.org/wiki/Information_hiding

Excerpt from this article:

摘自本文:

Information hiding in computer science is the principle of hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. The protection involves providing a stable interface which shields the remainder of the program from the implementation (the details that are most likely to change).

隐藏在计算机科学中的信息是将设计决策隐藏在最有可能改变的计算机程序中的原则,从而在设计决策改变时保护程序的其他部分不被改变。保护涉及提供稳定的接口,该接口将程序的其余部分与实现(最可能更改的细节)隔离开来。

One common form of encapsulation is using properties to hide private data fields. An even more common form is the use of OO to encapsulate the complexity of software into well divisoned classes with roles and responsibilities. This is a key tennant of OO, as it moves from a monolithic procedural design style to a more structured style which strives to hide all irrelevant information except that which pertains to the particular task your working on.

一种常见的封装形式是使用属性来隐藏私有数据字段。更常见的形式是使用OO将软件的复杂性封装到具有角色和职责的良好分类中。这是OO的一个关键角色,因为它从单一的程序设计风格转变为更加结构化的风格,它努力隐藏所有无关的信息,除了与你工作的特定任务有关的信息。

#2


It is my view and understanding that the term encapsulation (to encapsulate) is the art/science of capturing the essence of something for the purpose of display. In fact, by definition - to encapsulate is to package something or enclose it in another container. Therefore the term encapsulation would mean to take the essence of what you are attempting to achieve and packaging it in a useful form so that it can be reused as necessary.

我的观点和理解是,术语封装(封装)是为了展示而捕捉某物的本质的艺术/科学。事实上,根据定义 - 封装是封装东西或将其封装在另一个容器中。因此,术语封装将意味着采用您试图实现的内容的本质,并将其打包成有用的形式,以便可以在必要时重复使用。

So to interpret this, it would mean to package material in a form that would make it more useful later.

因此,为了解释这一点,它意味着将材料打包成一种形式,以便以后更有用。

So really...interpret this as you see fit. I see it as taking a bunch of algorithms and utilities and creating a class structure that can be used as an API in other projects. This encapsulated code could be inherited and/or extended to make it useful for modified purposes without changing the underlying essence of the API.

所以真的......按你认为合适的方式解释。我认为这需要一堆算法和实用程序,并创建一个可以在其他项目中用作API的类结构。可以继承和/或扩展此封装代码,以使其可用于修改目的,而无需更改API的基本要素。

Therefore, abstraction, polymorphism and inheritance aren't forms of encapsulation, but forms of extending and modifying encapsulated code.

因此,抽象,多态和继承不是封装的形式,而是扩展和修改封装代码的形式。

Different forms of encapsulation would mean the modifiers on properties, methods, fields and classes - that is public, private, static, virtual (in C#). Everything else (i.e. overloads, overrides, shadows) is a modification or an extension to that encapsulation.

不同形式的封装意味着属性,方法,字段和类的修饰符 - 即公共,私有,静态,虚拟(在C#中)。其他所有内容(即重载,覆盖,阴影)都是对该封装的修改或扩展。

You may consider the modified code an encapsulation which could then be further inherited/abstracted/extended, but the package which is to be extended is the encapsulated product.

您可以将修改后的代码视为可以进一步继承/抽象/扩展的封装,但要扩展的封装是封装产品。

#3


Encapsulation is defined by the International Organisation for Standardization's International Standard: "Information technology – Open Distributed Processing," ISO/IEC 10746, 1998.

封装由国际标准化组织的国际标准定义:“信息技术 - 开放分布式处理”,ISO / IEC 10746,1998。

It's defined in terms of more primitive definitions:

它是根据更原始的定义来定义的:

Entity: Any concrete or abstract thing of interest.

实体:任何感兴趣的具体或抽象事物。

Object: A model of an entity. An object is characterised by its behaviour and, dually, by its state.

对象:实体的模型。对象的特征在于其行为,并且双重地由其状态来表征。

Behaviour (of an object): A collection of actions with a set of constraints on when they may occur.

(对象的)行为:具有一组约束的动作集合,可以在何时发生。

Interface: An abstraction of the behaviour of an object that consists of a subset of the interactions of that object together with a set of constraints on when they may occur.

接口:对象行为的抽象,由对象的交互子集和可能发生的一组约束组成。

Encapsulation: the property that the information contained in an object is accessible only through interactions at the interfaces supported by the object.

封装:只能通过对象支持的接口上的交互来访问对象中包含的信息的属性。

The ISO does not define different types of encapsulation.

ISO没有定义不同类型的封装。

Other posts have mentioned information hiding. The ISO does not define encapsulation explicitly in terms of information hiding, though it does seem implicit, see "Encapsulation theory fundamentals," at http://www.edmundkirwan.com/pub/

其他帖子提到信息隐藏。 ISO没有明确地根据信息隐藏定义封装,尽管它看起来似乎是隐含的,请参阅http://www.edmundkirwan.com/pub/上的“封装理论基础”。

Ed.

#4


Encapsulation is more than simply information hiding. That is one aspect of it. It has to do with the interface to a module. An interface provides two very important functions: encapsulation and abstraction.

封装不仅仅是信息隐藏。这是它的一个方面。它与模块的接口有关。接口提供了两个非常重要的功能:封装和抽象。

Abstraction is when a client of a module does not need to know more than what is in the interface.

抽象是指模块的客户端不需要知道的内容多于接口中的内容。

and

Encapsulation is when a client of a module isn't able to know more than what is in the interface.

封装是指模块的客户端无法了解接口中的内容。

(Both definitions from Using UML by Perdita Stevens)

(Perdita Stevens使用UML的定义)

#5


Since encapsulation simply refers to "information hiding" then I would imagine that a lot of things can be categorized as encapsulation. However I tend to think of encapsulation as "implementation hiding", in other words it is a tool that I use to create loose coupling between anything I write and anything client of what I have written.

由于封装仅仅是指“信息隐藏”,因此我可以想象很多东西可以归类为封装。然而,我倾向于认为封装是“实现隐藏”,换句话说,它是一个工具,我用来创建我写的任何东西和我写的任何客户端之间的松散耦合。

So I tend to believe, pragmatically, that encapsulation is any paradigm or best-practice that allows me to present a clean, solid interface to any client.

因此,我倾向于相信,实际上,封装是任何范例或最佳实践,它允许我向任何客户端提供干净,可靠的接口。

#6


Generally the usage of the word is pretty close to what it says. You encapsulate something when you contain it, and don't let any of the deals loose. The best way to think about it is that you are taking something and putting it into a black-box where no one can see the details anymore. The box hides everything, providing some other disassociated interface in its place.

一般来说,这个词的用法非常接近它所说的。当你包含它时,你封装了一些东西,并且不要让任何交易松散。考虑它的最佳方式是你正在拿东西把它放进一个黑盒子里,再没人能看到细节了。盒子隐藏了一切,提供了一些其他不相关的界面。

Information hiding is just one aspect of encapsulation, since along with the data you can also hide any of the details of the code itself. The purpose of encapsulating a part of your system is to draw that bit of complexity away from the whole, thus making it easier to understand the separate details (on both sides). More?

信息隐藏只是封装的一个方面,因为除了数据之外,您还可以隐藏代码本身的任何细节。封装系统的一部分的目的是从整体上提取一点复杂性,从而更容易理解单独的细节(在两侧)。更多?

Paul.

#7


"Candidate Definitions for Encapsulation:

“封装的候选定义:

  • Physically grouping together related operations or things.
  • 将相关操作或事物物理分组。

  • GateKeeper of state or data.
  • GateKeeper的状态或数据。

  • Hiding implementation."

Sourced from: Encapsulation Definition

来源于:封装定义

There are two parts/ways to achieve Encapsulation:

有两个部分/方法来实现封装:

  • First, encapsulation is a technique that packages related data and behaviors into a single unit, i.e, Physical grouping of operations(behaviors)

    首先,封装是一种将相关数据和行为打包到一个单元中的技术,即操作的物理分组(行为)

E.g.:-

class Person {
    String name;
    int age;

    void talk() {
    }

    void think() {
    }

    void work() {
    }

    void play() {
    }
}
  • Second, encapsulation is a technique for protecting data from misuse by the outside world, which is referred as ‘information hiding’ or ‘data hiding’.

    其次,封装是一种保护数据免受外界滥用的技术,称为“信息隐藏”或“数据隐藏”。

E.g.:-

class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public String getAge() {
        return age;
    }
}

Sourced from: What is Encapsulation in Java - the WHAT, WHY and HOW, spoiler author cites Interface as an example, which is not true. Interface are for Abstraction

来自:什么是Java中的封装 - 什么是WHY,WHY和HOW,剧透作者引用Interface作为一个例子,这不是真的。接口用于抽象

#1


You're thinking too much I think.

你想的太多了。

http://en.wikipedia.org/wiki/Information_hiding

Excerpt from this article:

摘自本文:

Information hiding in computer science is the principle of hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. The protection involves providing a stable interface which shields the remainder of the program from the implementation (the details that are most likely to change).

隐藏在计算机科学中的信息是将设计决策隐藏在最有可能改变的计算机程序中的原则,从而在设计决策改变时保护程序的其他部分不被改变。保护涉及提供稳定的接口,该接口将程序的其余部分与实现(最可能更改的细节)隔离开来。

One common form of encapsulation is using properties to hide private data fields. An even more common form is the use of OO to encapsulate the complexity of software into well divisoned classes with roles and responsibilities. This is a key tennant of OO, as it moves from a monolithic procedural design style to a more structured style which strives to hide all irrelevant information except that which pertains to the particular task your working on.

一种常见的封装形式是使用属性来隐藏私有数据字段。更常见的形式是使用OO将软件的复杂性封装到具有角色和职责的良好分类中。这是OO的一个关键角色,因为它从单一的程序设计风格转变为更加结构化的风格,它努力隐藏所有无关的信息,除了与你工作的特定任务有关的信息。

#2


It is my view and understanding that the term encapsulation (to encapsulate) is the art/science of capturing the essence of something for the purpose of display. In fact, by definition - to encapsulate is to package something or enclose it in another container. Therefore the term encapsulation would mean to take the essence of what you are attempting to achieve and packaging it in a useful form so that it can be reused as necessary.

我的观点和理解是,术语封装(封装)是为了展示而捕捉某物的本质的艺术/科学。事实上,根据定义 - 封装是封装东西或将其封装在另一个容器中。因此,术语封装将意味着采用您试图实现的内容的本质,并将其打包成有用的形式,以便可以在必要时重复使用。

So to interpret this, it would mean to package material in a form that would make it more useful later.

因此,为了解释这一点,它意味着将材料打包成一种形式,以便以后更有用。

So really...interpret this as you see fit. I see it as taking a bunch of algorithms and utilities and creating a class structure that can be used as an API in other projects. This encapsulated code could be inherited and/or extended to make it useful for modified purposes without changing the underlying essence of the API.

所以真的......按你认为合适的方式解释。我认为这需要一堆算法和实用程序,并创建一个可以在其他项目中用作API的类结构。可以继承和/或扩展此封装代码,以使其可用于修改目的,而无需更改API的基本要素。

Therefore, abstraction, polymorphism and inheritance aren't forms of encapsulation, but forms of extending and modifying encapsulated code.

因此,抽象,多态和继承不是封装的形式,而是扩展和修改封装代码的形式。

Different forms of encapsulation would mean the modifiers on properties, methods, fields and classes - that is public, private, static, virtual (in C#). Everything else (i.e. overloads, overrides, shadows) is a modification or an extension to that encapsulation.

不同形式的封装意味着属性,方法,字段和类的修饰符 - 即公共,私有,静态,虚拟(在C#中)。其他所有内容(即重载,覆盖,阴影)都是对该封装的修改或扩展。

You may consider the modified code an encapsulation which could then be further inherited/abstracted/extended, but the package which is to be extended is the encapsulated product.

您可以将修改后的代码视为可以进一步继承/抽象/扩展的封装,但要扩展的封装是封装产品。

#3


Encapsulation is defined by the International Organisation for Standardization's International Standard: "Information technology – Open Distributed Processing," ISO/IEC 10746, 1998.

封装由国际标准化组织的国际标准定义:“信息技术 - 开放分布式处理”,ISO / IEC 10746,1998。

It's defined in terms of more primitive definitions:

它是根据更原始的定义来定义的:

Entity: Any concrete or abstract thing of interest.

实体:任何感兴趣的具体或抽象事物。

Object: A model of an entity. An object is characterised by its behaviour and, dually, by its state.

对象:实体的模型。对象的特征在于其行为,并且双重地由其状态来表征。

Behaviour (of an object): A collection of actions with a set of constraints on when they may occur.

(对象的)行为:具有一组约束的动作集合,可以在何时发生。

Interface: An abstraction of the behaviour of an object that consists of a subset of the interactions of that object together with a set of constraints on when they may occur.

接口:对象行为的抽象,由对象的交互子集和可能发生的一组约束组成。

Encapsulation: the property that the information contained in an object is accessible only through interactions at the interfaces supported by the object.

封装:只能通过对象支持的接口上的交互来访问对象中包含的信息的属性。

The ISO does not define different types of encapsulation.

ISO没有定义不同类型的封装。

Other posts have mentioned information hiding. The ISO does not define encapsulation explicitly in terms of information hiding, though it does seem implicit, see "Encapsulation theory fundamentals," at http://www.edmundkirwan.com/pub/

其他帖子提到信息隐藏。 ISO没有明确地根据信息隐藏定义封装,尽管它看起来似乎是隐含的,请参阅http://www.edmundkirwan.com/pub/上的“封装理论基础”。

Ed.

#4


Encapsulation is more than simply information hiding. That is one aspect of it. It has to do with the interface to a module. An interface provides two very important functions: encapsulation and abstraction.

封装不仅仅是信息隐藏。这是它的一个方面。它与模块的接口有关。接口提供了两个非常重要的功能:封装和抽象。

Abstraction is when a client of a module does not need to know more than what is in the interface.

抽象是指模块的客户端不需要知道的内容多于接口中的内容。

and

Encapsulation is when a client of a module isn't able to know more than what is in the interface.

封装是指模块的客户端无法了解接口中的内容。

(Both definitions from Using UML by Perdita Stevens)

(Perdita Stevens使用UML的定义)

#5


Since encapsulation simply refers to "information hiding" then I would imagine that a lot of things can be categorized as encapsulation. However I tend to think of encapsulation as "implementation hiding", in other words it is a tool that I use to create loose coupling between anything I write and anything client of what I have written.

由于封装仅仅是指“信息隐藏”,因此我可以想象很多东西可以归类为封装。然而,我倾向于认为封装是“实现隐藏”,换句话说,它是一个工具,我用来创建我写的任何东西和我写的任何客户端之间的松散耦合。

So I tend to believe, pragmatically, that encapsulation is any paradigm or best-practice that allows me to present a clean, solid interface to any client.

因此,我倾向于相信,实际上,封装是任何范例或最佳实践,它允许我向任何客户端提供干净,可靠的接口。

#6


Generally the usage of the word is pretty close to what it says. You encapsulate something when you contain it, and don't let any of the deals loose. The best way to think about it is that you are taking something and putting it into a black-box where no one can see the details anymore. The box hides everything, providing some other disassociated interface in its place.

一般来说,这个词的用法非常接近它所说的。当你包含它时,你封装了一些东西,并且不要让任何交易松散。考虑它的最佳方式是你正在拿东西把它放进一个黑盒子里,再没人能看到细节了。盒子隐藏了一切,提供了一些其他不相关的界面。

Information hiding is just one aspect of encapsulation, since along with the data you can also hide any of the details of the code itself. The purpose of encapsulating a part of your system is to draw that bit of complexity away from the whole, thus making it easier to understand the separate details (on both sides). More?

信息隐藏只是封装的一个方面,因为除了数据之外,您还可以隐藏代码本身的任何细节。封装系统的一部分的目的是从整体上提取一点复杂性,从而更容易理解单独的细节(在两侧)。更多?

Paul.

#7


"Candidate Definitions for Encapsulation:

“封装的候选定义:

  • Physically grouping together related operations or things.
  • 将相关操作或事物物理分组。

  • GateKeeper of state or data.
  • GateKeeper的状态或数据。

  • Hiding implementation."

Sourced from: Encapsulation Definition

来源于:封装定义

There are two parts/ways to achieve Encapsulation:

有两个部分/方法来实现封装:

  • First, encapsulation is a technique that packages related data and behaviors into a single unit, i.e, Physical grouping of operations(behaviors)

    首先,封装是一种将相关数据和行为打包到一个单元中的技术,即操作的物理分组(行为)

E.g.:-

class Person {
    String name;
    int age;

    void talk() {
    }

    void think() {
    }

    void work() {
    }

    void play() {
    }
}
  • Second, encapsulation is a technique for protecting data from misuse by the outside world, which is referred as ‘information hiding’ or ‘data hiding’.

    其次,封装是一种保护数据免受外界滥用的技术,称为“信息隐藏”或“数据隐藏”。

E.g.:-

class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public String getAge() {
        return age;
    }
}

Sourced from: What is Encapsulation in Java - the WHAT, WHY and HOW, spoiler author cites Interface as an example, which is not true. Interface are for Abstraction

来自:什么是Java中的封装 - 什么是WHY,WHY和HOW,剧透作者引用Interface作为一个例子,这不是真的。接口用于抽象