且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是多态性?它的用途是什么?

更新时间:2021-11-29 03:54:36

如果您考虑该术语的希腊词根,应该会很明显.

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

  • Poly = many:多边形=多面,聚苯乙烯=许多苯乙烯(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 :-)