这个问题已经在这里有了答案:
9年前关闭。
Possible Duplicate:
Abstraction VS Information Hiding VS Encapsulation
有人可以向我解释面向对象程序设计中封装和抽象原理之间的主要区别(如果可能的话,还可以提供示例)。
最佳答案
样本:
// NO ABSTRACTION, NO ENCAPSULATION
const int catLegs = 4;
const int spiderLegs = 8;
Leg[] catLegs;
Leg[] spiderLegs;
void MakeCatRun(Distance b) { for (int i=0; i<catLegs; ++i) catLegs[i] += b; }
void MakeSpiderRun(Distance b) { for (int i=0; i<spiderLegs; ++i) spiderLegs[i] += b; }
封装形式:
// ENCAPSULATION
class Cat
{
Leg[] legs;
int nLegs;
public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; }
}
class Spider
{
Leg[] legs;
int nLegs;
public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; }
}
抽象:
// ABSTRACTION
class LivingBeing
{
Leg[] legs;
int nLegs;
public void Run(Distance b) { for (int i=0; i < nLegs; ++i) leg[i] += b; }
}
class Cat: LivingBeing { }
class Spider: LivingBeing { }
关于oop - 封装和抽象概念之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3362335/