抽象和封装有何不同? [英] How abstraction and encapsulation differ?

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

问题描述

我正在准备面试,因此决定重新整理我的OOP概念. 有成百上千的文章,但似乎每个文章对它们的描述都不相同. 有些

I am preparing for an interview and decided to brush up my OOP concepts. There are hundreds of articles available, but it seems each describes them differently. Some says

抽象是识别具有以下特征的常见模式的过程: 系统变化;抽象代表通用模式 并提供了一种指定要使用哪个变体的方式"(Richard 加百利).

Abstraction is "the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use" (Richard Gabriel).

,并通过抽象类实现.

一些其他

抽象是指仅向客户显示必要的详细信息 对象

Abstraction means to show only the necessary details to the client of the object

假设您的Employee类中有一个"CalculateSalary"方法, 它以EmployeeId作为参数,并返回 当前月份的雇员,为整数值.现在如果有人 想要使用该方法.他不需要关心员工如何 对象计算薪水?他唯一需要关注的是 方法名称,其输入参数和结果格式 成员,

Let’s say you have a method "CalculateSalary" in your Employee class, which takes EmployeeId as parameter and returns the salary of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and format of resulting member,

我一次又一次地用Google搜索,结果似乎都没有给我一个正确的答案. 现在,封装在所有这些封装中适合什么地方? 我搜索并发现了一个堆栈溢出问题.甚至这些问题的答案都令人困惑 在这里,它说

I googled again and again and none of the results seem to give me a proper answer. Now, where does encapsulation fit in all these? I searched and found a stack overflow question. Even the answers to that questions were confusing Here, it says

封装是用作抽象的一部分的策略.封装形式 指对象的状态-对象封装了它们的状态,并且 从外面藏起来;该类的外部用户与之交互 通过其方法,但不能直接访问类状态.所以 该类抽象出与其相关的实现细节 状态.

Encapsulation is a strategy used as part of abstraction. Encapsulation refers to the state of objects - objects encapsulate their state and hide it from the outside; outside users of the class interact with it through its methods, but cannot access the classes state directly. So the class abstracts away the implementation details related to its state.

此处另一位知名会员说,

它们是不同的概念.

They are different concepts.

抽象是完善所有内容的过程 对象的不需要/不重要的属性,仅保留 最适合您的域的特征.

Abstraction is the process of refining away all the unneeded/unimportant attributes of an object and keep only the characteristics best suitable for your domain.

现在我把整个概念弄糊涂了.我了解抽象类,继承,访问说明符以及所有其他内容.我只是想知道在面试中被问到抽象和/或封装时我应该如何回答.

Now I m messed up with the whole concept. I know about abstract class, inheritance, access specifiers and all. I just want to know how should I answer when I am asked about abstraction and/or encapsulation in an interview.

请不要将其标记为重复.我知道有几个类似的问题.但是,我想避免相互矛盾的解释之间的混淆.谁能建议一个可靠的链接?也欢迎使用stackoverflow问题的链接,除非它再次造成混乱. :)

Please don't mark it as a duplicate. I know there are several similar questions. But I want to avoid the confusion among the conflicting explanations. Can anyone suggest a credible link? A link to stackoverflow question is also welcome unless it creates confusion again. :)

我需要答案,有点面向c#

推荐答案

抽象意味着只向对象的客户端显示必要的细节

Abstraction means to show only the necessary details to the client of the object

实际上就是封装.另请参阅Wikipedia文章的第一部分,以免被封装和数据隐藏所混淆. > http://en.wikipedia.org/wiki/Encapsulation_(面向对象的编程)

Actually that is encapsulation. also see the first part of the wikipedia article in order to not be confused by encapsulation and data hiding. http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

请记住,仅将所有您的类成员1:1隐藏在属性后面根本就不是封装.封装就是保护不变量和隐藏实现细节.

keep in mind that by simply hiding all you class members 1:1 behind properties is not encapsulation at all. encapsulation is all about protecting invariants and hiding of implementation details.

这是一篇很好的文章. http://blog.ploeh.dk/2012/11/27/Encapsulationofproperties/ 还可以查看该文章中链接的文章.

here a good article about that. http://blog.ploeh.dk/2012/11/27/Encapsulationofproperties/ also take a look at the articles linked in that article.

类,属性和访问修饰符是在c#中提供封装的工具.

classes, properties and access modifiers are tools to provide encapsulation in c#.

您做封装是为了降低复杂性.

you do encapsulation in order to reduce complexity.

抽象是识别具有系统变异的通用模式的过程;抽象表示通用模式,并提供了一种指定使用哪种变异的方法"(Richard Gabriel).

Abstraction is "the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use" (Richard Gabriel).

是的,这是抽象的一个很好的定义.

Yes, that is a good definition for abstraction.

它们是不同的概念. 抽象是完善对象的所有不需要/不重要属性,并仅保留最适合您的域的特征的过程.

They are different concepts. Abstraction is the process of refining away all the unneeded/unimportant attributes of an object and keep only the characteristics best suitable for your domain.

是的,它们是不同的概念.请记住,抽象实际上与使对象仅适合您的域相反.这是为了使对象适合一般的领域!

Yes, they are different concepts. keep in mind that abstraction is actually the opposite of making an object suitable for YOUR domain ONLY. it is in order to make the object suitable for the domain in general!

如果您遇到实际问题并提供特定的解决方案,则可以使用抽象来形式化更通用的解决方案,该解决方案也可以解决具有相同通用模式的更多问题.这样,您可以提高组件的可重用性,也可以使用其他程序员为相同域甚至不同域制作的组件.

if you have a actual problem and provide a specific solution, you can use abstraction to formalize a more generic solution that can also solve more problems that have the same common pattern. that way you can increase the re-usability for your components or use components made by other programmers that are made for the same domain, or even for different domains.

很好的例子是.net框架提供的类,例如列表或集合.这些是非常抽象的类,您几乎可以在任何地方和许多领域中使用.试想一下,如果.net仅实现了一个EmployeeList类和一个CompanyList,该类只能容纳具有特定属性的员工和公司列表.这样的类在很多情况下是没有用的.例如,如果您必须重新实现CarList的全部功能,那将是多么痛苦.因此,列表"远离员工,公司和汽车. List本身是一个抽象概念,可以通过其自己的类来实现.

good examples are classes provided by the .net framework, for example list or collection. these are very abstract classes that you can use almost everywhere and in a lot of domains. Imagine if .net only implemented a EmployeeList class and a CompanyList that could only hold a list of employees and companies with specific properties. such classes would be useless in a lot of cases. and what a pain would it be if you had to re-implement the whole functionality for a CarList for example. So the "List" is ABSTRACTED away from Employee, Company and Car. The List by itself is an abstract concept that can be implemented by its own class.

接口,抽象类或继承和多态性是在c#中提供抽象的工具.

Interfaces, abstract classes or inheritance and polymorphism are tools to provide abstraction in c#.

您进行抽象以提供可重用性.

you do abstraction in order to provide reusability.

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

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