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

前端 未结 28 2772
南笙
南笙 2020-11-21 07:08

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

28条回答
  •  -上瘾入骨i
    2020-11-21 07:52

    The term polymorphism comes from:

    poly = many

    morphism = the ability to change

    In programming, polymorphism is a "technique" that lets you "look" at an object as being more than one type of thing. For instance:

    A student object is also a person object. If you "look" (ie cast) at the student, you can probably ask for the student ID. You can't always do that with a person, right? (a person is not necessarily a student, thus might not have a student ID). However, a person probably has a name. A student does too.

    Bottom line, "looking" at the same object from different "angles" can give you different "perspectives" (ie different properties or methods)

    So this technique lets you build stuff that can be "looked" at from different angles.

    Why do we use polymorphism? For starters ... abstraction. At this point it should be enough info :)

提交回复
热议问题