抽象和封装之间有什么区别? [英] What is the difference between abstraction and encapsulation?

查看:116
本文介绍了抽象和封装之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

抽象和封装之间的区别?

究竟是什么Java中的封装和抽象之间的区别?任何简短的例子也将不胜感激。

What exactly is the difference between encapsulation and abstraction in Java? Any brief examples would also be appreciated.

推荐答案

抽象和封装是两种非常好吃的味道。

Abstraction and encapsulation are two great flavors that taste great together.

封装正在最大限度地减少您向代码用户公开的内容。 用户可能是您的代码的其余部分,或者使用您发布的代码的任何人。

Encapsulation is minimizing what you expose to the user of your code. That "user" may be the rest of your code, or whoever uses the code you publish.

封装有一些明确的好处:

There some definite benefits to encapsulation:


  • 代码的用户不依赖于程序中可能发生变化的部分。当您更改程序时,他们不必更改代码

  • 您可以更好地控制代码和状态在程序生命周期中的确切变化。你必须处理更少的场景,并且将有更少的意外问题需要修复

我不懂Java,但这里有一个小例子封装在C#中:

I don't know Java, but here is a small example of encapsulation in C#:

public class Giraffe
{
    public Giraffe(int heightInFeet)
    {
        this.heightInFeet = heightInFeet;
        this.numberOfSpots = heightInFeet * 72;
    }

    public override string ToString()
    {
        return "Height: " + heightInFeet + " feet"
            + " Number of Spots: " + numberOfSpots;
    }

    private int heightInFeet;
    private int numberOfSpots;
}

而不是公开 numberOfSpots ,它封装在类中,并通过 ToString 方法公开。

Instead of exposing numberOfSpots, it is encapsulated within the class, and exposed via the ToString method.

抽象正在使用扩展点让选择被推迟到运行精确代码的不同部分。这个选择可以在你的程序的其他地方,在另一个程序中,或在运行时动态地进行。

Abstraction is using extension points to let the choice be deferred to a different part of which exact code is run. That choice could be made elsewhere in your program, in another program, or dynamically at runtime.

抽象也有很大的好处:


  • 当您更改实现抽象的代码时,抽象的用户不必更改其代码。只要抽象没有改变,用户就不必更改代码。

  • 编写使用抽象的代码时,可以编写一次可重用的代码对任何实现该抽象的新代码。您可以编写更少的代码来执行更多操作。

C#中高度使用的抽象是 IEnumerable 。列表,数组,字典和任何其他类型的集合类都实现 IEnumerable foreach 循环结构和整个LINQ库都基于这种抽象:

A highly used abstraction in C# is IEnumerable. Lists, Arrays, Dictionaries, and any other type of collection class all implement IEnumerable. The foreach loop structure and the entirety of the LINQ library are based on that abstraction:

public IEnumerable<int> GetSomeCollection()
{
    // This could return any type of int collection.  Here it returns an array
    return new int[] { 5, 12, 7, 14, 2, 3, 7, 99 };
}

IEnumerable<int> someCollectionOfInts = GetSomeCollection();

IEnumerable<string> itemsLessThanFive = from i in someCollectionOfInts
                                        where i < 5
                                        select i.ToString();

foreach(string item in itemsLessThanFive)
{
    Console.WriteLine(item);
}

您也可以轻松编写自己的抽象:

You can easily write your own abstractions, too:

public interface IAnimal
{
    bool IsHealthy { get; }
    void Eat(IAnimal otherAnimal);
}

public class Lion : IAnimal
{
    public Lion()
    {
        this.isHealthy = true;
    }

    public bool IsHealthy
    {
        get { return isHealthy; }
    }

    void Eat(IAnimal otherAnimal)
    {
        if(otherAnimal.IsHealthy && !(otherAnimal is SlimeMold))
        {
            isHealthy = true;
        }
        else
        {
            isHealthy = false;
        }
    }

    private bool isHealthy;
}

IAnimal someAnimal = PullAnAnimalOutOfAWoodenCrate();

Console.WriteLine("The animal is healthy?: " + someAnimal.IsHealthy);

你可以同时使用它们,就像我用 IAnimal IsHealthy IAnimal 是一个缩写,并且只有获取访问者,并且没有设置 上的访问者> IsHealthy 是封装。

You can use both together, as I did with IAnimal, and IsHealthy. IAnimal is an abtraction, and having only a get accessor, and no set accessor on IsHealthy is encapsulation.

这篇关于抽象和封装之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆