首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多态是唯一的解决方案

多态是唯一的解决方案
EN

Stack Overflow用户
提问于 2016-05-10 04:58:01
回答 1查看 87关注 0票数 1

全部都是头衔。

我需要一个多态性是唯一解决方案的场景。我已经看到了许多具有多态性好处的线程,但我认为,缺少只有多态性的解决方案。

提出同样问题的另一种方式是:

“多态性的起源是什么?”

EN

回答 1

Stack Overflow用户

发布于 2016-05-10 16:01:40

你的问题在某种程度上可以解释。

让我试着回答你的问题,首先在以下几个方面划清界限:

  1. 多态性是一个抽象的概念
  2. 编程语言和编译器(如C++、Java、C#等)直接支持的多态性

多态性的默认示例可能是矢量图形工具,例如Corel绘图、Inkscape。假设您希望用户能够在画布上绘制几个不同的地理形状(方形、三角形、圆圈)。

我现在建议,多态的概念是解决这个问题的唯一解决方案,同时您当然能够用任何编程语言编写解决方案,包括那些没有提供一个完整的多态机制的解决方案(例如C)。

显式多态性将在几种“if”条件下变得明显。

因此:多态性的概念始终是唯一的解决办法,如果:

  1. 您有几个实体,它们共享公共行为的子集,但每个类也有一些特殊的行为,仅适用于该类。
  2. 编译时,您不知道需要什么具体的类,只有在运行时才知道。

关于多态性的“起源”问题的补充。多态的概念是很显赫的,比如Primes,至少我有这种感觉。在编程语言中对多态性的隐式支持始于90年代初的C++,早期的WYSWG程序,如Corel绘图、Word和这本书 (当然是个人观察)。

下面是一个例子。

代码语言:javascript
复制
// example WITH implicit polymorphism
abstract class PolyGeometricEntity
{
    public int center_x__mm;                    // commen superset
    public int center_y__mm;                    // commen superset
    public void move(int d_x__mm, int d_y_mm)   // commen superset
    {
        center_x__mm += d_x__mm;
        center_y__mm += d_y_mm:
    }

    public abstract int area();                 // commen superset on abstract level, but specialized at implementation level
    public abstract void draw();               // commen superset on abstract level, but specialized at implementation level
}

class CircleEntity : PolyGeometricEntity
{
    public override int area()
    {
        // circle specific
        return 1;
    }
    public override void draw()
    {
        // draw a circle
    }
}

class TriangleEntity : PolyGeometricEntity
{
    public override int area()
    {
        // triangle specific
        return 1;
    }
    public override void draw()
    {
        // draw a triangle
    }
}


class PolyCanvas
{
    List<PolyGeometricEntity> entities = new List<PolyGeometricEntity>();

    void CreateEntity(string toCreateClass)
    {
        // assume that code is called by the ui
        // you do not know what the user decides at runtime
        // Polymorphism 'starting' now:
        PolyGeometricEntity toCreate = null;
        if (toCreateClass == "c") { toCreate = new CircleEntity(); }
        else if (toCreateClass == "t") { toCreate = new TriangleEntity(); }
        entities.Add(toCreate);
    }

    void ReDraw()
    {
        foreach (PolyGeometricEntity toDraw in entities)
        {
            toDraw.draw(); // polymorphism in action!
        }
    }
}


// example WITHOUT implicit polymorphism
abstract class NonPolyGeometricEntity
{
    public int center_x__mm;                    // commen superset
    public int center_y__mm;                    // commen superset

    public string entityClass;                  

    public void move(int d_x__mm, int d_y_mm)   // commen superset
    {

    }

    // explicid Polymorphism, but concept of Polymorphism still present 
    public int area()
    {
        if (entityClass == "c")
        {
            // return area of cirlce
        }
        else if (entityClass == "t")
        {
            // return area of triangle
        }
        return 0;
    }
    public void draw()
    {
        if (entityClass == "c")
        {
            // draw area of cirlce
        }
        else if (entityClass == "t")
        {
            // draw area of triangle
        }
    }
}





class NonPolyCanvas
{
    List<NonPolyGeometricEntity> entities = new List<NonPolyGeometricEntity>();

    void CreateEntity(string toCreateClass)
    {
        // assume that code is called by the ui
        // you do not know what the user decides at runtime
        NonPolyGeometricEntity toCreate = null;
        toCreate.entityClass = toCreateClass;   //explit polymorphism
        entities.Add(toCreate);
    }

    void ReDraw()
    {
        foreach (NonPolyGeometricEntity toDraw in entities)
        {
            toDraw.draw(); 
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37129590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档