抽象和封装之间的C#区别 [英] C# difference between Abstraction and Encapsulation

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

问题描述

我只是花了很长一段时间在谷歌试图解决这个问题,并希望确保我做对了。



封装是隐藏另一个程序员的数据不同的类不应该能够直接访问。



抽象只提供类中必不可少的方法和功能。 AKA我可能有30种方法可以使我的班级工作,但是任何人都可以访问它们中的5个,因为他们会对其他功能进行所有必要的调用。



因此,封装是私有变量和方法,而抽象是公共变量。这是正确的吗?



我知道抽象也可以使用抽象,接口和重载来完成。我只是想知道我是否在最基本的意义上。

解决方案

好的,你在谷歌搜索方面效率不高:)

封装 [ ^ ]正如你所草拟的那样:从用户类中隐藏他不关心的内容。如果我实现了一个表示多项式的类,请不要让用户使用存储coefficeints的数组。等等...

如果您想了解抽象 [ ^ ],你最好还看另一个方向,专业化。让我们考虑一下形状。形状是一种抽象的东西,但所有形状都有面积和周长。但在这一点上你不能说如何计算这些。 Bur你有矩形,三角形,弯曲的和一般的多边形。这些是形状抽象的专门后代。此时,您可以计算它们的周长和周长。但你可以走得更远。正方形是一个专用矩形,您可以使用更专业的矩形覆盖矩形类中定义的公式。还有专门的三角形和专门的曲线形状。



如果您阅读我链接的文章,您将会看到,计算机结构中还有其他类型的抽象。但是这个想法是一样的:抽象是处理与真实单词对象相关的模型的复杂性,摆脱不感兴趣的方面,并专注于重要的事情的方式。但请记住,建模不是一次性操作:同一个真实单词对象的抽象模型可能因项目而异。


我认为你正在努力,因为很多C#和OOP的新手,尝试使用实现你可能称之为通用OOP哲学的地图大图概念/结果到你编程的实际C#代码对象。



根据我的经验,学生经常从C#开始,他们普遍认为OOP是关于在现实世界中建模对象的。并且,是的,这是OOP技术的一个主要用途,但是,在C#编程中,您也在处理代码所特有的对象。你在WinForms中的程序是一个(静态)类;你在WinForms中的UserControl可以从其他用户界面小部件继承。



我想建议你专注于学习和创建实现的C#程序。使用类(抽象,虚拟和常规),接口,结构,枚举和代理。



随着您越来越多地掌握使用这些工具/物品,我建议您退后一步,明确表达对您的理解,如何适应您对OOP的理解, 一般来说。这个建议是基于我与许多学生的经验,其中大多数学生通过自下而上的方式更好地学习OOP,而不是自上而下的方法。



抽象是一个原则,要实现的结果,以可以有效互动的方式实现对象关系,并且不会产生未知的副作用。



继承是用于实现抽象的关键技术; C#的继承工具允许单类继承和多接口继承(其他编程语言提供不同的范例)。但是,在C#中,实际上可以通过复合或串行继承形式实现多重继承:

  public  Class1 
{
public void Method1( string sParam, int iParam){}
}

public Class2:Class1
{
public void Method2( string sParam, int iParam){}
}

public Class3:Class2
{
public void Method3( string sParam, int iParam){}
}

在这个(愚蠢的)例子中,重点是任何实例o f Class3可以访问Class1中的Method1和Class2中的Method2。



C#中的实际抽象类不一定是抽象,尽管它是一个可以使用的工具用来实现抽象:记住,抽象类可以包含实际的方法实现,以及虚方法。



封装是一个通过调节物体与其他物体的相互作用,对物体可能的相互作用进行严格控制,并通过正式手段(例如,接口)来规范暴露,这是一个重要的概念,它说它是有价值的,可取的,甚至是必要的。这个大画面的概念也用模块化,关注点分离等词来表达。



围绕术语OOP,有一大堆流行语来来去去,反映时代,趋势,最新的大师。



用C#允许,不允许,脚踏实地是一个稍后,探索扩展方法(甚至可以扩展接口!),Linq,Reflection和动态创建包含可执行代码的新程BLOCKQUOTE>

I just spent a long while on Google trying to figure this out and want to make sure I have it right.

Encapsulation is hiding data that another programmer of different classes should not be able to directly access.

Abstraction is only making available the methods and features that are essential of a class. AKA I may have 30 methods that are necessary for making my class work, but only 5 of them should be accessed by anyone calling it, as they make all the necessary calls to the other functions.

So Encapsulation are the private variables and methods and Abstraction are the public ones. Is this correct?

I understand that Abstraction can be completed using Abstract, Interface and overloading as well. I just want to know if I'm right in the most basic sense.

解决方案

Ok, you were not quite efficient in googling :)
Encapsulation[^] is quite what you have drafted: hiding from the class "user" what's not his concern. If I implement a class that represents a polynom, don't let the user uf my class mess around with the array storing the coefficeints. And so on...
If you wnat to understand abstraction[^], you better also look at the other direction, specialization. Let's think about shapes. A shape is an abstract thing, but all shapes have area and perimeter. But at this point you can't say how to calculate these. Bur you have rectangles, triangles, curved ones, and general polygons. These are specialized descendants of the shape abstraction. At this point you can calculate are and perimeter of them. But you can go even further. A square is a specialized rectangle, and you can override the formulas defined in the rectangle class, with more specialized ones. Aswell there are specialized triangles and specialized curved shapes.

If you read the article I have linked you will see, that there are other kinds of abstraction in computer sciances. But the idea is the same: abstraction is the way of dealing with the compexity of a model related to the real-word objects, getting rid of uninteresting aspects, and concentrating on what's important. But keep in mind that modelling is not a one-time action: the abstract model of the same real-word object might differ from project to project.


I think you are struggling, as many newcomers to C# and OOP do, with trying map big-picture concepts/outcomes of implementing what you might call "general OOP philosophy" to actual C# code objects you program with.

In my experience students often start C# with a general sense that OOP is about modeling "objects" in "the real world;" and, yes, that is a major use of OOP technique, but, in programming in C# you are dealing with objects that are unique to code, as well. Your Program in WinForms is a (static) Class; your UserControl in WinForms can inherit from other user-interface widgets.

I'd like to suggest that you focus on studying, and creating, actual C# programs that implement and use Classes (abstract, virtual, and "regular"), Interfaces, Structs, Enums, and Delegates.

As you gain increasing mastery of using these tools/objects, then I suggest you step-back and articulate how, for you, they fit in to your understanding of OOP, in general. This suggestion is based on my experience with many students, most of whom do better learning OOP with a bottom-up, rather than top-down approach.

Abstraction is a principle, an outcome to be attained, of implementing relationships of objects in such a way that they can interact efficiently, and also do not generate unknown side-effects.

Inheritance is the key technique used to implement abstraction; C#'s inheritance facility allows for single-class inheritance, and multiple interface inheritance (other programming languages offer different paradigms). However, one can, in C#, in effect achieve multiple inheritance by compound, or serial, inheritance of the form:

public Class1
{
    public void Method1(string sParam, int iParam) {}
}

public Class2 : Class1
{
    public void Method2(string sParam, int iParam) {}
}

public Class3 : Class2
{
    public void Method3(string sParam, int iParam) {}
}

In this (silly) example the point is that any instance of Class3 will have access to Method1 in Class1 and Method2 in Class2.

An actual Abstract Class in C# is not necessarily an "abstraction," although it's a tool one can use to implement "abstraction:" remember that the Abstract Class can contain actual Method implementations, as well as Virtual methods.

Encapsulation is a big-picture concept that says it is valuable, desirable, even necessary, to exert rigorous control over the possible interactions of objects by regulating what they expose to other objects, and regulate that exposure through formal means (Interfaces, for example). This big-picture concept is also expressed using terms like "modularity," "separation of concerns."

Around the term "OOP," there are a swarm of buzzwords that come and go, reflecting the times, the trends, the latest gurus.

Getting "down to earth" with what C# allows, and does not allow, is a good basis for, later, exploring such fascinating capabilities of the language as Extension Methods (you can even extend Interfaces !), Linq, Reflection and dynamic creating of new assemblies at run-time that contain code that can be executed, etc.


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

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