封装和抽象之间的确切区别是什么?
当前回答
简而言之:
抽象是一种帮助我们识别哪些特定信息是必要的,哪些信息应该隐藏的技术。
因此,封装是一种以隐藏对象的细节和实现细节的方式封装信息的技术。
其他回答
抽象或封装并不严格要求信息隐藏。信息可能会被忽略,但不一定要隐藏。
封装是一种将事物视为单一事物的能力,即使它可能由许多复杂的部分或思想组成。例如,我可以说我坐在一把“椅子”上,而不是指这把椅子上的许多不同部分,每个部分都有特定的设计和功能,它们精确地组合在一起,目的是让我的屁股舒适地离地板几英尺远。
Abstraction is enabled by encapsulation. Because we encapsulate objects, we can think about them as things which relate to each other in some way rather than getting bogged down in the subtle details of internal object structure. Abstraction is the ability to consider the bigger picture, removed from concern over little details. The root of the word is abstract as in the summary that appears at the top of a scholarly paper, not abstract as in a class which can only be instantiated as a derived subclass.
I can honestly say that when I plop my butt down in my chair, I never think about how the structure of that chair will catch and hold my weight. It's a decent enough chair that I don't have to worry about those details. So I can turn my attention toward my computer. And again, I don't think about the component parts of my computer. I'm just looking at a part of a webpage that represents a text area that I can type in, and I'm communicating in words, barely even thinking about how my fingers always find the right letters so quickly on the keyboard, and how the connection is ultimately made between tapping these keys and posting to this forum. This is the great power of abstraction. Because the lower levels of the system can be trusted to work with consistency and precision, we have attention to spare for greater work.
两者之间存在差异
ABSTRAACTION
and
封装
1. First difference between Abstraction and Encapsulation is that, Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifier.
2. Data abstraction simply means generalizing something to hide the complex logic that goes underneath where Encapsulation is DATA HIDING.
3. Encapsulation is combining related logic data (variables and methods) where as Abstraction is hiding internal implementation details and expose only relevant details to the user. In a way you can Abstraction is achieved by Encapsulation.
封装:对对象的实际用户隐藏不需要的/不期望的/适当的实现细节。 如。
List<string> list = new List<string>();
list.Sort(); /* Here, which sorting algorithm is used and hows its
implemented is not useful to the user who wants to perform sort, that's
why its hidden from the user of list. */
抽象:是一种提供泛化的方法,因此是处理大量不同对象的通用方法。如。
class Aeroplane : IFlyable, IFuelable, IMachine
{ // Aeroplane's Design says:
// Aeroplane is a flying object
// Aeroplane can be fueled
// Aeroplane is a Machine
}
// But the code related to Pilot, or Driver of Aeroplane is not bothered
// about Machine or Fuel. Hence,
// pilot code:
IFlyable flyingObj = new Aeroplane();
flyingObj.Fly();
// fighter Pilot related code
IFlyable flyingObj2 = new FighterAeroplane();
flyingObj2.Fly();
// UFO related code
IFlyable ufoObj = new UFO();
ufoObj.Fly();
// **All the 3 Above codes are genaralized using IFlyable,
// Interface Abstraction**
// Fly related code knows how to fly, irrespective of the type of
// flying object they are.
// Similarly, Fuel related code:
// Fueling an Aeroplane
IFuelable fuelableObj = new Aeroplane();
fuelableObj.FillFuel();
// Fueling a Car
IFuelable fuelableObj2 = new Car(); // class Car : IFuelable { }
fuelableObj2.FillFuel();
// ** Fueling code does not need know what kind of vehicle it is, so far
// as it can Fill Fuel**
抽象
抽象是提取所有现有和可预见实现的公共属性和字段的过程。
例如: 轿车是轿车、掀背车、SUV、双门跑车、敞篷车的抽象概念。 Car将具有所有类型的汽车所共有的所有属性和字段。
封装
封装是对用户隐藏不需要的细节的过程。这个术语来自于胶囊。就像药物隐藏在胶囊里一样。各种机器和设备和设备的细节,从搅拌机,自行车,洗衣机,收音机,电视到飞机。您不希望用户可以看到机器的所有细节。
在编程方面: 让我们考虑一个等级汽车。 在下面的例子中,所有用户需要知道的是转动键(turnKey()方法),他不知道内部函数。用户不需要了解任何内部功能或内部组件。
在这种情况下,所有的私有方法都是内部函数,像“活塞p1”这样的私有字段是用户不需要知道的内部数据。
public class Car{
private void startMotor(){ //do something }
private void generateVoltage(){ //do something }
private void sparkPlugIgnition(){ //do something }
private void fuelFlowFromTankToInjector(){ //do something }
private void pushPistonsDown() {
p1.doAction();
p2.doAction();
//do something }
private void moveCrankShaft(){ //do something }
private Piston p1;
private Piston p2;
public void turnKey(){
startMotor();
generateVoltage();
sparkPlugIgnition();
fuelFlowFromTankToInjector();
pushPistonsDown();
moveCrankShat();
...
}
}
许多答案和例子都具有误导性。
封装是将“数据”和“对该数据进行操作的函数”打包到单个组件中,并限制对某些对象组件的访问。 封装意味着对象的内部表示通常隐藏在对象定义之外的视图中。
抽象是一种表示基本特性而不包括实现细节的机制。
封装:——信息隐藏。 抽象:——实现隐藏。
示例(c++):
class foo{
private:
int a, b;
public:
foo(int x=0, int y=0): a(x), b(y) {}
int add(){
return a+b;
}
}
foo类的任何对象的内部表示都隐藏在该类的外部。——>封装。 foo对象的任何可访问成员(data/function)都是受限的,只能由该对象访问。
foo foo_obj(3, 4);
int sum = foo_obj.add();
方法add的实现是隐藏的。——>抽象。