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

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

问题描述

我已阅读过一些我可以在互联网上找到的有关多态性的文章。但我认为我无法完全理解它的意义及其重要性。大多数文章没有说明为什么它很重要,以及如何在OOP中实现多态行为(当然在JavaScript中)。



我无法提供任何代码示例,因为我还没有想法如何实现它,所以我的问题如下:


  1. 它是什么?

  2. 为什么我们需要它?

  3. 它如何工作?

  4. 如何在javascript中实现这种多态行为?

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

  function Person(age,weight){
this。年龄=年龄;
this.weight = weight;
this.getInfo = function(){
returnI am+ this.age +years old+
and weights+ this.weight +kilo。;


函数员工(年龄,体重,薪水){
this.salary =薪水;
this.age =年龄;
this.weight = weight;
this.getInfo = function(){
returnI am+ this.age +years old+
and weights+ this.weight +kilo+
并赚取+ this.salary +美元。
}
}

Employee.prototype = new Person();
Employee.prototype.constructor =员工;
//参数'obj'可以是任何形式
//要执行的方法getInfo()取决于对象
//'obj'指的是。

函数showInfo(obj){
document.write(obj.getInfo()+< br>);
}

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


解决方案

多态是面向对象编程的一个原则OOP)。这是设计对象以共享行为并能够用特定行为覆盖共享行为的做法。多态性利用继承来实现此目的。



在OOP中,所有东西都被认为是一个对象。这种抽象可以一直贯穿到汽车的螺母和螺栓上,或者像一辆汽车一样宽泛,只有一年,制造和型号。



有一个多态的汽车情景会有基本的汽车类型,然后会有继承自汽车的子类,并提供他们自己的行为,汽车将具有的基本行为。例如,一个子类可能是TowTruck,它仍然会有年份制作和模型,但也可能有一些额外的行为和属性,这些行为和属性可能与IsTowing的标志一样基本,像电梯的细节一样复杂。



回到员工和员工的榜样,所有员工都是员工,但所有员工都不是员工。也就是说人们将成为超级班,并为员工分班。人们可能有年龄和体重,但他们没有薪水。员工是人,所以他们本质上会有年龄和体重,但也是因为他们是员工,他们会有薪水。所以为了方便这个,我们会先写出超类(Person)

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

我们将给予个人分享他们信息的能力

  Person.prototype.getInfo = function(){
returnI am+ this.age +years old+
并称+ this.weight +公斤。;
};

接下来,我们希望拥有Person,Employee

的一个子类

 函数Employee(年龄,体重,薪水){
this.age = age;
this.weight = weight;
this.salary =工资;
}
Employee.prototype = new Person();

我们将通过定义一个更适合Employee的函数来覆盖getInfo的行为

  Employee.prototype.getInfo = function(){
returnI am+ this.age +years old+
,并权衡+ this.weight +kilo+
并获得+ this.salary +美元。
};

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

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

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

然而,在这里使用继承没有太多的收获,因为Employee的构造函数与人类非常相似,原型中唯一的功能被覆盖。多态设计的力量是分享行为。

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. What is it?
  2. Why we need it ?
  3. How it works?
  4. How can I achieve this polymorphic behavior in 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);

解决方案

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.

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.

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.

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;
}

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.";
};

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();

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());

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天全站免登陆