65.9K
CodeProject 正在变化。 阅读更多。
Home

JavaScript 中的多态性

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.85/5 (9投票s)

2012年1月16日

CPOL

2分钟阅读

viewsIcon

106142

JavaScript 中多态是如何工作的。

引言

面向对象编程中的多态是创建具有多种形式的变量、函数或对象的能力。

背景

面向对象编程中多态的主要用途是属于不同类型的对象能够响应同名的方法、字段或属性调用,每个对象根据适当的类型特定行为进行响应。

程序员不必提前知道对象的准确类型,因此确切的行为在运行时确定。 这就是我们所说的后期绑定或动态绑定(这使得多态性成为一个非常酷的功能)。 要求是,所有超类、子类和接口中必须具有同名和相同参数集的属性。

使用代码

就 JavaScript 而言,我们实际上没有像其他编程语言那样的接口,但我们有超类和子类。 回到我关于 JavaScript 继承的文章,我们已经创建了从其他类继承的类。

让我们重用相同的例子,这次我们将创建一个 Person 类,然后创建另一个名为 Manager 的类,该类从 Person 类继承。 这两个类都将包含一个名为 wake_up() 的方法。 然后,我们将创建这两种类型的对象并将它们放入一个数组中。

当我们循环遍历该数组时,系统将在每个对象上调用函数 wake_up(),并且实现将动态确定。

让我们定义我们的 Person 类。

  Person = function (id, name, age) {
            this.id = id;
            this.name = name;
            this.age = age;
           // alert('A new person has been accepted');
        }
        /* definition of our Person class */
        Person.prototype = {
            /** wake person up */
            wake_up: function () {
                alert('A person is awake');
            },
            /** retrieve person's age */
            get_age: function () {
                return this.age;
            }
        }

现在让我们创建一个继承类,以便其他类可以从我们的 Person 类继承。 有关继承的更多详细信息和演示,请参阅我关于JavaScript 继承文章,点击这里

Inheritance_Manager = {};  
           
Inheritance_Manager.extend = function (subClass, baseClass) { 
                function inheritance() { }
                inheritance.prototype = baseClass.prototype;
                subClass.prototype = new inheritance();
                subClass.prototype.constructor = subClass;
                subClass.baseConstructor = baseClass;
                subClass.superClass = baseClass.prototype; }

好的,然后我们将按如下方式定义我们的 Manager 类

Manager = function (id, name, age, salary) {
          Manager.baseConstructor.call(this, id, name, age);
          this.salary = salary;
          // alert('A manager has been registered.'); 
}

我们的 Manager 类将按如下方式从 Person 类继承

Inheritance_Manager.extend(Manager, Person);

让我们添加更多功能并覆盖我们的 wake_up() 函数

 Manager.prototype = {
    wake_up: function () {
        alert('I am in control with Polymorphism');
    }
 }

现在我们创建一个数组并在其中存储 Person 和 Manager 类型的对象。

var arrPeople = new Array();
arrPeople[0] = new Person(1, 'Joe Tester', 26);
arrPeople[1] = new Manager(1, 'Joe Tester', 26, '20.000');

看看函数 wake_up() 如何根据我们拥有的不同对象来表现。 这就是我们所说的多态。 当您有许多呈现相同接口但具有不同实现的对象时,多态性使事情变得非常简单。

for (var i in arrPeople) {
   arrPeople[i].wake_up();
   alert(arrPeople[i].get_age());
}

许多人并不认为 JavaScript 是一个完全成熟的面向对象编程语言,但我认为 JavaScript 只是以自己的方式做事,这与其他语言不同。 正如我们在这里在多态的情况下所看到的,与其他编程语言没有区别。 所以它完全是多态的。

这是完整的代码

     /** This is our Person class */
            Person = function (id, name, age) {
                this.id = id;
                this.name = name;
                this.age = age;
               // alert('A new person has been accepted');
            }
            /* definition of our Person class */
            Person.prototype = {
                /** wake person up */
                wake_up: function () {
                    alert('A person is awake');
                },
                /** retrieve person's age */
                get_age: function () {
                    return this.age;
                }
            }    

            Inheritance_Manager = {};   

            Inheritance_Manager.extend = function (subClass, baseClass) { 
                function inheritance() { }
                inheritance.prototype = baseClass.prototype;
                subClass.prototype = new inheritance();
                subClass.prototype.constructor = subClass;
                subClass.baseConstructor = baseClass;
                subClass.superClass = baseClass.prototype;
            }    

            Manager = function (id, name, age, salary) {
                Manager.baseConstructor.call(this, id, name, age);
                this.salary = salary;
               // alert('A manager has been registered.');
            }    

            Inheritance_Manager.extend(Manager, Person);    

            Manager.prototype = {
                wake_up: function () {
                    alert('I am in control');
                }
            }    

            var arrPeople = new Array(); 
            arrPeople[0] = new Person(1, 'Joe Tester', 26);
            arrPeople[1] = new Manager(1, 'Joe Tester', 26, '20.000');    

            for (var i in arrPeople) {
                arrPeople[i].wake_up();
                alert(arrPeople[i].get_age());
            }
© . All rights reserved.