什么是多态性,它的用途是什么,它是如何使用的? [英] What is polymorphism, what is it for, and how is it used?

查看:21
本文介绍了什么是多态性,它的用途是什么,它是如何使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是多态性,它的用途是什么,如何使用?

What is polymorphism, what is it for, and how is it used?

推荐答案

如果您考虑该术语的希腊词根,它应该变得显而易见.

If you think about the Greek roots of the term, it should become obvious.

  • Poly = 多:多边形 = 多面,聚苯乙烯 = 许多苯乙烯 (a),polyglot = 多种语言,等等.
  • Morph = 变化或形式:形态 = 研究生物形式,Morpheus = 希腊梦之神,可以采取任何形式.
  • Poly = many: polygon = many-sided, polystyrene = many styrenes (a), polyglot = many languages, and so on.
  • Morph = change or form: morphology = study of biological form, Morpheus = the Greek god of dreams able to take any form.

因此,多态性是(在编程中)为不同的底层形式(数据类型)呈现相同接口的能力.

So polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).

例如,在许多语言中,整数和浮点数是隐式多态的,因为您可以进行加、减、乘等操作,而不管类型是否不同.在通常的术语中,它们很少被视为对象.

For example, in many languages, integers and floats are implicitly polymorphic since you can add, subtract, multiply and so on, irrespective of the fact that the types are different. They're rarely considered as objects in the usual term.

但是,以同样的方式,BigDecimalRationalImaginary 之类的类也可以提供这些操作,即使它们对不同的数据类型.

But, in that same way, a class like BigDecimal or Rational or Imaginary can also provide those operations, even though they operate on different data types.

经典的例子是 Shape 类以及所有可以从它继承的类(正方形、圆形、十二面体、不规则多边形、splat 等).

The classic example is the Shape class and all the classes that can inherit from it (square, circle, dodecahedron, irregular polygon, splat and so on).

通过多态,这些类中的每一个都有不同的底层数据.一个点形状只需要两个坐标(当然,假设它在二维空间中).一个圆需要一个圆心和半径.正方形或矩形需要左上角和右下角的两个坐标以及(可能)旋转.一个不规则的多边形需要一系列的线.

With polymorphism, each of these classes will have different underlying data. A point shape needs only two co-ordinates (assuming it's in a two-dimensional space of course). A circle needs a center and radius. A square or rectangle needs two co-ordinates for the top left and bottom right corners and (possibly) a rotation. An irregular polygon needs a series of lines.

通过让类负责其代码和数据,您可以实现多态.在这个例子中,每个类都有自己的 Draw() 函数,客户端代码可以简单地做:

By making the class responsible for its code as well as its data, you can achieve polymorphism. In this example, every class would have its own Draw() function and the client code could simply do:

shape.Draw()

获得任何形状的正确行为.

to get the correct behavior for any shape.

这与代码与数据分离的旧方式形成对比,您将拥有诸如 drawSquare()drawCircle().

This is in contrast to the old way of doing things in which the code was separate from the data, and you would have had functions such as drawSquare() and drawCircle().

面向对象、多态和继承都是密切相关的概念,了解它们至关重要.在我漫长的职业生涯中,有过许多银弹",它们基本上都以失败告终,但结果证明面向对象范式是一个很好的方法.学习它,理解它,热爱它——你会很高兴你做到了 :-)

Object orientation, polymorphism and inheritance are all closely-related concepts and they're vital to know. There have been many "silver bullets" during my long career which basically just fizzled out but the OO paradigm has turned out to be a good one. Learn it, understand it, love it - you'll be glad you did :-)

(a) 我最初是作为一个笑话写的,但结果证明是正确的,因此并没有那么好笑.单体苯乙烯恰好是由碳和氢组成,C8H8,而聚苯乙烯则是由它们的基团组成,(C8H8)n.

(a) I originally wrote that as a joke but it turned out to be correct and, therefore, not that funny. The monomer styrene happens to be made from carbon and hydrogen, C8H8, and polystyrene is made from groups of that, (C8H8)n.

也许我应该说息肉是字母 p 的多次出现,尽管现在我不得不解释这个笑话,即使这看起来也不好笑.

Perhaps I should have stated that a polyp was many occurrences of the letter p although, now that I've had to explain the joke, even that doesn't seem funny either.

有时,您应该在落后时退出:-)

Sometimes, you should just quit while you're behind :-)

这篇关于什么是多态性,它的用途是什么,它是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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