抽象和封装与实时示例有什么区别 [英] What is the difference between abstraction and encapsulation with real time example

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

问题描述

阅读了这么多关于此的文章后我感到困惑。为了实现封装(或数据隐藏)概念,我们使用访问说明符,并且对于证明抽象,我们使用访问说明符。亲切的,有人解释我如何实现代码。



我尝试过:



我尝试了以下链接:

http://www.codeproject.com/Articles/1037139/Difference-between-Encapsulation-and-Abstraction-i

解决方案

像胶囊(药片)一样封装,将药物藏在里面。封装包装,只是隐藏属性和方法。封装 使用 隐藏代码和数据 一个单位来保护来自世界之外的数据。类封装的最佳示例。 

封装隐藏变量或某些可能经常更改的实现 a class 以防止外人直接访问它。他们必须通过getter和setter方法访问它。

抽象是指仅向目标用户显示必要的详细信息。顾名思义,抽象 抽象形式任何。我们在编程语言中使用抽象来使 abstract class 。 Abstract class 表示 视图 class <的方法和属性/跨度>。

抽象 用于隐藏某些内容,但 a 更高的学位( class interface 。客户使用抽象 (或 interface 不关心它是谁或它是什么,他们只需要知道它可以做什么

封装和抽象之间的实现差异

1 使用 interface class =code-keyword> abstract class while Encapsulation 使用
private > protected 访问修饰符。

2 。 OOPS利用封装来强制执行类型的完整性(即确保数据 通过阻止程序员来自以非预期的方式访问数据 。通过封装,只有预定的 group 函数可以访问数据。对于
数据类型和操作(方法)的集合术语与访问限制(公共/私有等)捆绑在一起 a class

3 。封装示例
public int 标记
{
获取 {返回标记; }
set {marks = value ;}
}
< span class =code-digit> 4
。抽象示例

abstract class 抽象{ public abstract void Abstractionfun(); }

public class AbstractionImpl:Abstraction
{
public void Abstractionfun(){ // 实施}
}


封装是关于隐藏实现:集合类它隐藏了外部世界的实际集合,并提供了自己的方法来访问集合内容。想想购物清单或音乐播放列表:只要你能说我现在买这个,或者跳到下一个轨道,一切都很好,你就不关心应用程序如何存储它。封装把东西放在一个盒子里并为你提供一个小洞,你可以通过它与他们互动 - 你不能伸手并开始抓住东西! :笑:



抽象是不同的 - 它是关于概括而不是隐藏。您使用抽象通过忽略细节将相关项分组在一起,并将它们留给单独的实现。例如,想想一辆汽车 - 它是一个抽象的数量,它具有共同的属性和方法,而不管你可以驾驶的个别具体例子。它可以具有轮数属性,即驱动方法。但它不需要知道 - 甚至不关心 - 发动机是汽油,柴油,电动,混合动力,弹性带还是不存在。这些都是实现细节,这是实现类的关注点,而不是抽象基础。

因此抽象的Car对象无法驱动,但所有福特,梅赛德斯,起亚,...的例子一辆车可以 - 没有你需要重新训练来驾驶它们。



看看谷歌 - 那里有两个很好的详细解释超出我们可以放在像这样的小文本框中的方式! :笑:

After read so many article on this I got confuse. To implement encapsulation (or data hiding) concept we are using access specifier and also for prove Abstraction we are using access specifier. kindly someone explain me how i can implement on code.

What I have tried:

I tried with bellow links :
http://www.codeproject.com/Articles/1037139/Difference-between-Encapsulation-and-Abstraction-i

解决方案

Encapsulation like a capsule (medicine tablet) which hides medicine inside it. Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation.
 
Encapsulate hides variables or some implementation that may be changed so often in a class to prevent outsiders access it directly. They must access it via getter and setter methods.
 
Abstraction refers to showing only the necessary details to the intended user. As the name suggests, abstraction is the "abstract form of anything". We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.
 
Abstraction is used to hiding something too but in a higher degree(class, interface). Clients use an abstract class(or interface) do not care about who or which it was, they just need to know what it can do.
 
Implementation Difference Between Encapsulation and Abstraction
 
1. Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.
 
2. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.
 
3. Example of Encapsulation
    public int Marks 
    { 
      get { return marks; } 
      set { marks = value;} 
    } 
4. Example of Abstraction
 
   abstract class Abstraction { public abstract void Abstractionfun(); }
 
   public class AbstractionImpl: Abstraction 
   { 
       public void Abstractionfun() { //Implementation } 
   }


Encapsulation is about hiding the implementation: a collection class which hides the actual collection from the "outside world" and provides it's own methods to access the collection content for example. Think of a shopping list, or a music play list: you don;t care how the application stores it as long as you can say "I bought this now", or "skip to next track" everything is fine. Encapsulation "puts things in a box" and provides you will a small hole through which you can interact with them - you can't "put your hand in" and start grabbing things! :laugh:

Abstraction is different - it's about generalizing rather than hiding. You use abstraction to "group" related items together by ignoring details, and leaving them up to the individual implementation. For example, think about a Car - it's an abstract quantity, which has common properties and methods regardless of the individual "concrete" example you can drive. It can have a "number of wheels" property, a "Drive" method. But it doesn't need to know - or even care - whether the engine is petrol, diesel, electric, hybrid, elastic bands, or nonexistent. Those are all implementation details which are the concern of implementation classes, not the abstract base.
So the abstract Car object can't be driven, but all Ford, Mercedes, Kia, ... examples of a Car can - without you having to be re-trained to drive them.

Have a look at google - there are some good detailed explanations of both out there that go way beyond what we can put in a little text box like this! :laugh:


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

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