ES6类执行多态性的能力 [英] ES6 Classes ability to perform polymorphism
问题描述
我正在尝试通过ES6类来模拟多态性,以便能够更好地理解该理论.
I am trying to emulate polymorphism through ES6 Classes, in order to be able to better understand this theory.
概念很明确(设计对象以共享行为,并能够用特定行为覆盖共享行为),但我担心上面的代码不是有效的多态示例.
Concept is clear (designing objects to share behaviors and to be able to override shared behaviors with specific ones) but I am afraid my code above is not a valid polymorphism example.
由于我缺乏经验,如果您以全面的方式回答以下问题,我将不胜感激:
Due to my lack of experience, I appreciate if you answer these questions in a comprehensive way:
- 两个类都有一个同名的方法,并且每个类创建的实例都可以正确访问各自的方法,这使它成为一个多态示例吗?
- 如果这不效仿多态,应该进行哪些更改在代码中完成了吗?
- 我尝试删除
Employee.prototype = new Person();
行,但该行仍然有效.这就是为什么我担心没有这个概念的原因.
- The fact that both Classes have an equally named method, and that instances made from each Class get access correctly to their respective methods, makes this a polymorphic example?
- In case this does not emulate polimorphism, what changes should be done in the code for it?
- I've tried removing the
Employee.prototype = new Person();
line, and it still works. This is why I am afraid I'm not getting this concept.
class Person {
constructor (name, age) {
this._name = name;
this._age = age;
}
}
Person.prototype.showInfo = function(){
return "Im " + this._name + ", aged " + this._age;
};
class Employee {
constructor (name, age, sex) {
this._name = name;
this._age = age;
this._sex = sex;
}
}
Employee.prototype = new Person();
Employee.prototype.showInfo = function(){
return "Im " + this._sex + ", named " + this._name + ", aged " + this._age;
};
var myPerson = new Person('Jon', 20);
var myEmployee = new Employee('Doe', 10, 'men');
document.write(myPerson.showInfo() + "<br><br>"); // Im Jon, aged 20
document.write(myEmployee.showInfo() + "<br><br>"); // Im men, named Doe, aged 10
推荐答案
每个JavaScript对象都有一个内部的原型"属性,通常称为[[prototype]],该属性指向它直接继承的对象.
Every JavaScript object has an internal "prototype" property, often called [[prototype]], which points to the object from which it directly inherits.
每个JavaScript函数[object]都有一个属性原型,该原型由[几乎]空的对象初始化.当通过将其作为构造函数调用来创建此函数的新实例时,该新对象的[[prototype]]将指向构造函数的原型对象.
Every JavaScript function [object] has a property prototype, which is initialized with an [nearly] empty object. When you create a new instance of this function by calling it as a constructor, the [[prototype]] of that new object will point to the constructor's prototype object.
因此,当您编写此 var myPerson = new Person('Jon',20);
时,您将拥有showInfo方法,因为您拥有
So, when you write this var myPerson = new Person('Jon', 20);
,you have the method showInfo because you have this
Person.prototype.showInfo = function(){
return "Im " + this._name + ", aged " + this._age;
};
使用ES6,如果您想了解多态性,可以这样做:
With ES6 if you want to see the polymorphism you could do that :
class Person {
constructor (name, age) {
this._name = name;
this._age = age;
}
showInfo () {
return "Im " + this._name + ", aged " + this._age;
}
}
class Employee extends Person {
constructor (name, age, sex) {
super(name,age);
this._sex = sex;
}
showInfo(){
return "Im " + this._sex + ", named " + this._name + ", aged " + this._age;
}
}
var myPerson = new Person('Jon', 20);
var myEmployee = new Employee('Doe', 10, 'men');
document.write(myPerson.showInfo() + "<br><br>"); // Im Jon, aged 20
document.write(myEmployee.showInfo() + "<br><br>"); // Im men, named Doe, aged 10
这篇关于ES6类执行多态性的能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!