封装与抽象 [英] Encapsulation versus Abstraction

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

问题描述

好像我已经失去了基础知识,但两者之间却真的很困惑.区分封装和抽象的问题的核心是什么?

据我了解:
封装是将功能,方法,属性,事件等绑定到具有适当抽象级别(访问修饰符)的一个胶囊或类中.



抽象是使用访问修饰符隐藏数据.

这是一个小孩的定义.我想要两者之间完全合格的专业区别.

请帮助我让自己的立场正确0:doh:

Looks like I have lost my basics but really confused between the two. What is the core of the matter that differentiates the Encapsulation from Abstraction?

As far as I get it:
Encapsulation is binding of function, method, properties, events etc. in one capsule or Class with appropriate level of abstraction (access modifiers)

and

Abstraction is hiding of data using access modifier.

This is a kiddish definition. I want a fully qualified professional difference between the two.

Please help me to get my ground 0 right :doh:

推荐答案

Hello)
我将尝试展示一种观点.
您知道,现实世界中的对象通常具有数百万个属性和方法.但这并不意味着您将在您的程序中使用所有这些对象.相反,您仅使用您需要与程序上下文相关的那些对象. ,如果您编写用于计算薪水的应用程序,则您将不需要人体的这种属性,例如体重的身高,反之亦然,在医疗诊所编写应用程序时,您将明确需要这些参数.因此,简而言之,抽象就是当您决定您将在特定程序中使用的属性,以及您不会使用的属性.

现在有关封装.
从名称可以猜到,它只是隐藏了用户(胶囊)的尺寸.其作用是-使用户无法影响该过程.例如-对于开车的人来说,启动引擎只是旋转钥匙的问题.但是对于制造商而言,这是一个相当复杂的过程-首先将其加入触点,然后打开起动器,然后产生火花,然后使引擎运转.在编程方面-例如,如果您想验证电子邮件地址,则u应该封装在该验证过程中使用的私有变量(以防止修改它们并得到错误的答案()).
Hello)
I will try to show a point of view.
u see, objects in real world usually have millions of properties and methods.but it does not mean that u will use all of them in your programm.Oppositly,u use only those that u need accordin to the context of your programm.for example,if u write application that calculates the salary,u will not need such properties of human like height of weight,and vise versa,writing apllication for medical clinic,u will definetly need those parameters.So,in few words, abstraction is when u decide which properties u will use in particular programm,and which u wont.

now about encapsulation.
as it can be guessed from the name,its just a hiding the reslisation from user(capsule).what for - to make the user unable to affect the process.for example - for car driwer starting the engine is just a matter of spinning key.but for manufacturer it is rather complicated procedure - firstly it joins contacts,then turning on starter,then candle generates spark - and finally engine is working.incapsulation makes sure that driwer wont interrupt that complicated process(cuz there is a chanse that otherwise engine will be broken).In terms of programming - if u want,for example,validate the email-adress,u should encapsulate private variables that are used within that validation procedure(to prevent modifiring them and getting wrong answer ()).


谢谢您的提问.

封装对最终用户隐藏了实现.示例:

Thank you for your question.

Encapsulation is hiding implementation from end user. Example:

String str1="Hello";
Console.Write(str1.Length);



在上面的示例中, str1 字符串实例对象向客户端隐藏了 Length 的实现细节.

抽象是构建对象所需的一切.在现实世界中,要制造汽车,您需要轮子,颜色,方向盘,镜面玻璃等.提取汽车对象需要所有一切.

示例:



In the above example, str1 String instance object hide implementation details of Length from client.

Abstraction is everything that need to build an object. In real world consideration, to build a car you need wheels, color, steering wheels, looking glass etc. All things are needed to abstraction a car object.

Example:

public class Car
{
   int _wheel;
   String _color;
   int _glass;

   public int Wheel
   {
     get {return _wheel;}
     set { _wheel=value;}
   }

  public String Color
  {
     get {return _color;}
     set { _wheel=color;}
  }

  public int Glass
  {
     get {return _glass;}
     set { _glass=value;}
  }
}



这个例子让您明白了吗?


谢谢,
Mamun



Has this example made you clear?


Thanks,
Mamun


抽象:

抽象是隐藏实现细节并显示基本功能的过程.

示例1:笔记本电脑包含许多东西,例如处理器,主板,RAM,键盘,LCD屏幕,无线天线,网络摄像机,usb端口,电池,扬声器等.要使用它,您无需了解LCD的内部结构屏幕,键盘,网络摄像机,电池,无线天线,扬声器的作品.您只需要知道如何通过打开笔记本电脑来操作笔记本电脑.考虑一下您是否必须在操作笔记本电脑之前致电了解笔记本电脑所有内部详细信息的工程师.这将非常昂贵,而且每个人都不容易在任何地方使用.

因此,此处的笔记本电脑旨在隐藏其复杂性.
如何抽象:-通过使用访问说明符

.Net具有五个访问说明符

公共-可通过对象引用在类外部访问.

私有-仅可通过成员函数在类内部访问.

受保护的-就像私有的一样,但也可以通过成员
在派生类中访问 功能.

内部-在装配体内部可见.通过对象可访问.

受保护的内部-通过对象可见在装配内部以及通过成员函数在装配外部的派生类中可见.


封装:-

封装是将数据成员和成员函数绑定到单个单元中的过程.

封装的示例是class.一个类可以包含数据结构和方法.
考虑下面的类

公共类光圈
{
公共光圈()
{
}
受保护的双高度;
受保护的双倍宽度;
受保护的双厚度;
公共双倍获取音量()
{
双倍体积=高*宽*厚;
如果(volume< 0)
返回0;
返回量;
}
}

在此示例中,我们封装了一些数据,例如高度,宽度,厚度和获取体积"方法.其他方法或对象可以通过具有公共访问修饰符的方法与此对象进行交互
Abstraction:

Abstraction is a process of hiding the implementation details and displaying the essential features.

Example1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don''t need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works. You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone.

So here the Laptop is an object that is designed to hide its complexity.
How to abstract: - By using Access Specifiers

.Net has five access Specifiers

Public -- Accessible outside the class through object reference.

Private -- Accessible inside the class only through member functions.

Protected -- Just like private but Accessible in derived classes also through member
functions.

Internal -- Visible inside the assembly. Accessible through objects.

Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.


Encapsulation:-

Encapsulation is a process of binding the data members and member functions into a single unit.

Example for encapsulation is class. A class can contain data structures and methods.
Consider the following class

public class Aperture
{
public Aperture ()
{
}
protected double height;
protected double width;
protected double thickness;
public double get volume()
{
Double volume=height * width * thickness;
if (volume<0)
return 0;
return volume;
}
}

In this example we encapsulate some data such as height, width, thickness and method Get Volume. Other methods or objects can interact with this object through methods that have public access modifier


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

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