我已经阅读了一些我可以在互联网上找到的关于多态性的文章.但我认为我无法理解它的含义及其重要性.大多数文章没有说明为什么它很重要以及如何在OOP中实现多态行为(当然在JavaScript中).
我无法提供任何代码示例,因为我还没有想到如何实现它,所以我的问题如下:
我有这个例子.但是很容易理解这段代码会产生什么结果.它没有给出关于多态性本身的任何清晰的想法.
function Person(age, weight) {
this.age = age;
this.weight = weight;
this.getInfo = function() {
return "I am " + this.age + " years old " +
"and weighs " + this.weight +" kilo.";
}
}
function Employee(age, weight, salary) {
this.salary = salary;
this.age = age;
this.weight = weight;
this.getInfo = function() {
return "I am " + this.age + " years old " +
"and weighs " + this.weight +" …Run Code Online (Sandbox Code Playgroud) javascript oop polymorphism functional-programming parametric-polymorphism