本文介绍了如何在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

推荐答案

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.



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

11-01 14:49