gpt4 book ai didi

c# - 抽象和封装有何不同?

转载 作者:IT王子 更新时间:2023-10-29 03:43:14 28 4
gpt4 key购买 nike

我正在准备面试,因此决定复习一下我的 OOP 概念。有数百篇文章可用,但似乎每篇文章的描述都不一样。 Some

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).

并且是通过抽象类实现的。

一些 other

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

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,

我用谷歌搜索了一遍又一遍,但似乎没有一个结果能给我一个正确的答案。现在,封装在所有这些方面适合什么地方?我搜索并找到了一个 stack overflow question .甚至这些问题的答案也令人困惑 Here , 它说

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.

here另一位知名成员(member)说,

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.

现在我搞砸了整个概念。我知道抽象类、继承、访问说明符等等。我只想知道在面试中被问及抽象和/或封装时我应该如何回答。

请不要将其标记为重复。我知道有几个类似的问题。但我想避免相互矛盾的解释之间的混淆。任何人都可以建议一个可靠的链接吗?也欢迎链接到 stackoverflow 问题,除非它再次造成混淆。 :)

编辑:我需要答案,有点面向 C#

最佳答案

封装:使用getters和setters等隐藏数据

抽象:使用抽象类和接口(interface)等隐藏实现

关于c# - 抽象和封装有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16938667/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com