封装与信息隐藏 [英] Encapsulation vs Information hiding

查看:214
本文介绍了封装与信息隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ecapsulation和信息隐藏之间到底有什么区别?



我知道将字段设为私有然后制作字段的setter和getter是ecapsulation.However确实封装意味着这个吗?



假设我有一个如下所述的课程。

  public Class IsThisEncapsulation 
{
public int age;

public void setAge(int age)
{
this.age = age;
}

public int getAge()
{
返回年龄;
}
}

现在IsThisEncapsulation类是Encapsulation的一个例子吗? / p>

现在将上述类私有的字段'age'隐藏起来吗?



你能不能给我清楚的例子可以帮助我清楚地区分这些概念吗?

解决方案


我知道制作字段私有,然后制作字段的setter和getter是封装。但是,封装意味着这个吗?


--->封装是一个OOP概念,其中对象状态(类字段)及其行为(方法)被包裹在一起。 Java使用 class 提供封装。


信息隐藏:


- >限制访问某些对象组件的机制。您的上述示例是信息隐藏的情况,如果您使年龄私有。






最初,信息/数据隐藏被认为是封装的一部分,封装的定义如下:




  • 一种语言限制访问某些对象组件的机制。

  • 一种语言结构,它有助于将数据与对该数据进行操作的方法(或其他函数)捆绑在一起。



第二个定义的动机是,在许多OOP语言中隐藏组件不是自动的或可以被覆盖;因此,信息隐藏被那些喜欢第二个定义的人定义为一个单独的概念。



参考: wikipage


What exactly are the differences between Ecapsulation and Information Hiding?

Well i know that making fields private and then making setter and getter of the fields is ecapsulation.However does encapsulation mean just this?

suppose i have a class as described below.

public Class IsThisEncapsulation
{
    public int age;

    public void setAge(int age)
    {
       this.age=age;
    }

    public int getAge()
    {
       return age;
    }
}

Now is the class IsThisEncapsulation an example of Encapsulation?

Now would making the field 'age' in the above class private achieve informaton hiding?

Could you please give me clear examples that will help me distinguish these concepts clearly?

解决方案

Well I know that making fields private and then making setter and getter of the fields is encapsulation. However, does encapsulation mean just this?

---> Encapsulation is an OOP concept where object state(class fields) and it's behaviour(methods) is wrapped together. Java provides encapsulation using class.

Information Hiding:

--> mechanism for restricting access to some of the object's components. Your above example is the case of Information Hiding if you make age private.


Initially, Information/Data Hiding was considered the part of Encapsulation, and the definitions of Encapsulation would be as:

  • A language mechanism for restricting access to some of the object's components.
  • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

the second definition is motivated by the fact that in many OOP languages hiding of components is not automatic or can be overridden; thus, information hiding is defined as a separate notion by those who prefer the second definition.

Reference: wikipage

这篇关于封装与信息隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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