oop - 封装和抽象概念之间的区别

标签 oop encapsulation abstraction

这个问题已经在这里有了答案:




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/

相关文章:

c++ - 使用数组时的抽象与性能

java - 我如何检查数组列表是否包含某个字符串

php - 无法从数组中的类访问静态成员变量

c++ - 在 C++ header 中转发声明 C typedef

java - 如何避免 getter 并避免对 UI 进行硬编码?

javascript - 在函数内调用 javascript 函数

postgresql - 抽象 PSQL 写查询

java - 实现一个调用接口(interface)方法的类?

php - 是否可以有一个具有私有(private)/ protected 方法的接口(interface)?

php - Laravel 资源 Controller Slug