什么是 Javascript 中的多态性? [英] What is polymorphism in Javascript?

查看:25
本文介绍了什么是 Javascript 中的多态性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些我可以在互联网上找到的关于多态性的可能的文章.但我想我不能完全理解它的含义和重要性.大多数文章没有说明为什么它很重要以及我如何在 OOP 中(当然在 JavaScript 中)实现多态行为.

I have read some possible article I could found on the internet on polymorphism. But I think I could not quite grasp the meaning of it and its importance. Most of the articles don't say why it is important and how I can achieve polymorphic behavior in OOP (of course in JavaScript).

我无法提供任何代码示例,因为我不知道如何实现它,所以我的问题如下:

I can not provide any code example because I haven't got the idea how to implement it, so my questions are below:

  1. 是什么?
  2. 我们为什么需要它?
  3. 它是如何工作的?
  4. 如何在 javascript 中实现这种多态行为?

我有这个例子.但是很容易理解这段代码的结果.它没有给出关于多态性本身的任何清晰概念.

I have got this example. But it is easily understandable what will be outcome of this code. It doesn't give any clear idea about polymorphism itself.

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 +" kilo " +
        "and earns " + this.salary + " dollar.";
    }
}

Employee.prototype = new Person();
Employee.prototype.constructor = Employee;
  // The argument, 'obj', can be of any kind
  // which method, getInfo(), to be executed depend on the object
  // that 'obj' refer to.

function showInfo(obj) {
    document.write(obj.getInfo() + "<br>");
}

var person = new Person(50,90);
var employee = new Employee(43,80,50000);
showInfo(person);
showInfo(employee);

推荐答案

多态是面向对象编程 (OOP) 的原则之一.它是设计对象以共享行为并能够用特定的行为覆盖共享行为的实践.多态利用继承来实现这一点.

Polymorphism is one of the tenets of Object Oriented Programming (OOP). It is the practice of designing objects to share behaviors and to be able to override shared behaviors with specific ones. Polymorphism takes advantage of inheritance in order to make this happen.

在 OOP 中,一切都被认为是作为对象建模的.这种抽象可以一直深入到汽车的具体细节,也可以简单地概括为具有年份、品牌和型号的汽车类型.

In OOP everything is considered to be modeled as an object. This abstraction can be taken all the way down to nuts and bolts for a car, or as broad as simply a car type with a year, make, and model.

要拥有多态汽车场景,将有基本汽车类型,然后会有从汽车继承的子类,并在汽车具有的基本行为之上提供自己的行为.例如,一个子类可能是 TowTruck,它仍然有一个年份的品牌和型号,但也可能有一些额外的行为和属性,这些行为和属性可能像 IsTowing 的标志一样基本,也可能像电梯的细节一样复杂.

To have a polymorphic car scenario there would be the base car type, and then there would subclasses which would inherit from car and provide their own behaviors on top of the basic behaviors a car would have. For example, a subclass could be TowTruck which would still have a year make and model, but might also have some extra behaviors and properties which could be as basic as a flag for IsTowing to as complicated as the specifics of the lift.

回到人和员工的例子,所有的员工都是人,但所有的人都不是员工.也就是说,人是超类,员工是子类.人们可能有年龄和体重,但他们没有薪水.员工是人,所以他们天生就有年龄和体重,但也因为他们是员工,所以他们会有薪水.

Getting back to the example of people and employees, all employees are people, but all people are not employees. Which is to say that people will be the super class, and employee the sub class. People may have ages and weights, but they do not have salaries. Employees are people so they will inherently have an age and weight, but also because they are employees they will have a salary.

所以为了方便,我们先写出超类(Person)

So in order to facilitate this, we will first write out the super class (Person)

function Person(age,weight){
 this.age = age;
 this.weight = weight;
}

我们将赋予 Person 共享其信息的能力

And we will give Person the ability to share their information

Person.prototype.getInfo = function(){
 return "I am " + this.age + " years old " +
    "and weighs " + this.weight +" kilo.";
};

接下来我们希望有一个 Person, Employee 的子类

Next we wish to have a subclass of Person, Employee

function Employee(age,weight,salary){
 this.age = age;
 this.weight = weight;
 this.salary = salary;
}
Employee.prototype = new Person();

我们将通过定义一个更适合员工的行为来覆盖 getInfo 的行为

And we will override the behavior of getInfo by defining one which is more fitting to an Employee

Employee.prototype.getInfo = function(){
 return "I am " + this.age + " years old " +
    "and weighs " + this.weight +" kilo " +
    "and earns " + this.salary + " dollar.";  
};

这些可以类似于您的原始代码使用

These can be used similar to your original code use

var person = new Person(50,90);
var employee = new Employee(43,80,50000);

console.log(person.getInfo());
console.log(employee.getInfo());

然而,这里使用继承并没有太多好处,因为Employee的构造函数与person的构造函数如此相似,并且原型中唯一的函数是被覆盖的.多态设计的力量在于共享行为.

However, there isn't much gained using inheritance here as Employee's constructor is so similar to person's, and the only function in the prototype is being overridden. The power in polymorphic design is to share behaviors.

这篇关于什么是 Javascript 中的多态性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆