如何在ASP.NET中达到抽象 [英] How abstraction can be acheive in asp.net

查看:65
本文介绍了如何在ASP.NET中达到抽象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为封装可以通过类似的属性来实现,所以我想知道我们
在ASP.NET程序中实现抽象化

as encapsulation can be acheive by properties similaraly i want to know that how we
acheive abstraction in asp.net program

推荐答案

抽象化hiding the implementation details并显示基本功能的过程.

简而言之:
.抽象正在以某种方式思考某些事情
.抽象仅表示对象的基本特征
并隐藏了对象的本质特征.
.通过抽象可以隐藏所有相关数据,以降低复杂性
并提高效率
.抽象通过对适合
的类进行建模来简化复杂的现实. 问题
.抽象-外部布局,用于设计
.封装保护抽象.
.它获取所需的数据并隐藏不需要的数据.

抽象表示您拥有比扩展它的其他类更通用的类.例如,如果您有Triangle和Rectangle类:
Abstraction is a process of hiding the implementation details and displaying the essential features.

In short:
. Abstraction is thinking about something a certain way
. Abstraction is the representation of only the essential features of an object
and hiding un essential features of an object.
. Through Abstraction all relevant data can be hide in order to reduce complexity
and increase efficiency
. Abstraction is simplifying complex reality by modeling classes appropriate to
the problem
. Abstraction-outer layout, used in terms of design
. Encapsulation protects abstraction.
. It taking required data and hiding the unwanted data.

Abstraction means that you have some class that is more common than others that extend it. By example, if you have classes Triangle and Rectangle:
class Triangle
{
    public double a;
    public double b;
    public double c;


    public double Area
    {
       get { return triangle's area }
    }
}

class Rectangle
{
    public double a;
    public double b;


    public double Area
    {
       get { return rectangle's area }
    }
}


这些类有一些共同点-它们具有称为Area 的属性,还具有sides (a和b)的属性,但是我看不出有任何理由使侧面抽象.
让我们也添加Circle ,我们一点都没有.现在让我们归纳这些类,并创建一个更常见的类,称为Shape.
另外,让我们将其设为abstract 类,这样就不能单独创建该类-只能通过扩展来创建它..


These classes have something in common - they have property called Area plus also sides (a and b) but I don''t see any reason to make sides abstract.
Let''s add also Circle and we have no point of sides at all. Now let''s generalize these classes and let''s create one more common class called Shape.
Also, let''s make it abstract class so this class cannot be created separately - it can be created only through extending.

public abstract class Shape
{
    public double Area();
}


现在让我们编写以前的类,以便它们扩展Shape类.


And now let''s write previous classes so they extend Shape class.

class Triangle : Shape
{
    public double a;
    public double b;
    public double c;

    public double Area
    {
       get { return triangle's area }
    }
}

class Rectangle : Shape
{
    public double a;
    public double b;

    public double Area
    {
       get { return rectangle's area }
    }
}


这些类中没有一个使用Shape 作为其基类.
Circle也是如此.
在我们不关心对象的特定属性的情况下,我们可以将所有扩展对象作为Shape处理.
例如,让我们计算列表中Shapes 总面积.


Not of these classes use Shape as their base class.
So does Circle.
In the context where we don ''t care about specific properties of object we can handle all extended objects as Shape.
By example, let''s calculate total area of Shapes in list.

List<Shape> shapes = ListShapes() // contains circles, triangles and rectangles
double area = 0;

foreach(Shape shape in shapes)
   area += shape.Area;

// do something useful with area here


在不扩展Shape 类的情况下,我们应该为每种形状类型编写单独的循环.而且我们还必须有单独的方法来获得不同类型的形状.
现在,我们只有一种方法可以列出形状,而只有一种方法可以将它们作为一种形式处理-我们不在乎边的长度或半径或任何其他特定的属性.同样,您可以进行许多其他抽象.
例如,如果需要定位形状,则可以将坐标添加到Shape类.


Without extending Shape class we should write separate loop for each shape type. And also we have to have separate methods to get different types of shapes.
Now we had only one method to list shapes and one loop to handle them as one - we didn''t cared about lengths of sides or radiuses or any other specific properties. Same way you can make many other abstractions.
By example, you can add coordinates to Shape class if shape''s positioning is required.


C#中的抽象类意味着*必须*是子类.它本身不能实例化.同样,抽象方法也需要在子类中放入代码.

抽象类的一个好用法是,如果您想实现一个类将需要的大多数功能,但是出于某些原因,不应直接使用该类.

例如,我有一个ComboBox类.它基本上处理了可以从Codes DataSet填充自身所需的一切,但是在设计时,它永远无法知道Codes DataSet的来源.因此,它必须由子类来确定,子类将在设计时知道它将使用哪个Codes DataSet(然后,开发人员将为需要ComboBox的每个Codes DataSet创建一个子类. ).

因此,为了使开发人员不会尝试直接使用该ComboBox类,我将其抽象化,因此必须对其进行子类化.有道理吗?
An abstract class in C# means that the class *has* to be sub-classed. It can not be instantiated itself. Likewise an abstract method needs to have code put into it in the sub-class.

A good use for an abstract class is if you want to implement the majority of the functionality that a class will need, but there are reasons that the class shouldn''t be used directly.

As an example, I have a ComboBox class. It basically handles everything necessary to be able to fill itself from a Codes DataSet, but it will not ever be able to, at design-time, know where that Codes DataSet comes from. Consequently, it is something that *must* be determined by a sub-class, which will know at design-time which Codes DataSet it will use (and the developer will then create a sub-class for every Codes DataSet they need a ComboBox for).

So, to make it so that a developer will not attempt to use that ComboBox class directly, I have made it abstract and so it must be sub-classed. Make sense?


这篇关于如何在ASP.NET中达到抽象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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