问题
I see abstraction in processes. I see abstraction in data.
I see that abstraction is losing the unimportant details.
I see that abstraction is giving a group of elements a name and treating them as one unit. (But I don't know why that is considered abstraction. So, please I need clarification on this particular point)
I know there are also levels of abstraction, and although the name implies something, I don't have a practical example, and I can't think of a specific one I'm confused about the definition of abstraction.
Can somebody write a comprehensive article? Scratch that. Can somebody give a comprehensive answer?
EDIT:- Thank you for your answers. However, I was looking for a generalized answer. For example, I'm reading an article in which procedures are considered abstractions. However, here the answers are about abstract classes in C# and Java, (until now.) Thank you again.
回答1:
Abstraction is the technique of hiding implementation. At it's core there's not much more to that answer. The bulk of meaning to abstraction come from how and why it is used.
It is used for the following scenarios
- Reduce complexity. (Create a simple interface)
- Allow for implementation to be modified without impacting its users.
- Create a common interface to support polymorphism (treating all implementations of the abstracted layer the same.
- Force users to extend the implementation rather than modify.
- Support cross platform by changing the implementation per platform.
回答2:
Quite simply, abstraction is the art of limiting your dependencies. The less you depend on, the more abstract you are. For example, if you write a DirectX renderer, then you're abstracted from what graphics card vendor and model you're running on. If you write another layer, you can be insulated from what OS you're running on.
回答3:
Abstraction is hiding details of specific implementations and share common details among implementations. Example is java.util.List, java.util.ArrayList and java.util.Map. List is the parent (the abstraction), ArrayList and Map are specific implementation.
You want to do this whenever you have shared code between different classes, so that you don't repeat your code which is bad.
Abstraction is very useful in code reuse, dynamic behavior and standardization. For example, there is a method that you are using and it accepts a List, so to use this method, you can send any object that has list as its parent. Now inside this method, there could be different implementations depending on what is the type of the passed object, so you can achieve a dynamic behavior at run-time. This is very useful technique when you design a framework.
回答4:
I am not sure if you are supposed to recommend books and if so let me know and I will delete my post but I like Pro C# by Troelsen. Abstraction is like an Interfaces but Interfaces do not allow you to define constructor(s). It is for generalizing. Like I have a grid I want to display some user fields in. The fields can be text, enumeration, single-value, multi-value. I built an abstract class FieldDef with an abstract string property DispValue. Then the various field type inherit from FieldDef. In the grid I have simple string to display. Then when the user updates a field properties and methods specific to the field type are exposed. The other example is all mammals have common proprieties but as you drill down you expose more properties but there is a single generalized view (interface) for all mammals and by inheriting from mammals there is a way to search across and display properties common to all mammals.
来源:https://stackoverflow.com/questions/7028242/what-is-abstraction