What is the real significance(use) of polymorphism

前端 未结 10 2329
别跟我提以往
别跟我提以往 2020-12-01 01:44

I am new to OOP. Though I understand what polymorphism is, but I can\'t get the real use of it. I can have functions with different name. Why should I try to implement polym

10条回答
  •  遥遥无期
    2020-12-01 02:17

    Polymorphism allows you to write code that uses objects. You can then later create new classes that your existing code can use with no modification.

    For example, suppose you have a function Lib2Groc(vehicle) that directs a vehicle from the library to the grocery store. It needs to tell vehicles to turn left, so it can call TurnLeft() on the vehicle object among other things. Then if someone later invents a new vehicle, like a hovercraft, it can be used by Lib2Groc with no modification.

提交回复
热议问题