Pet*_*ter 32 javascript polymorphism inheritance
我是一名程序员,他使用多种语言进行编程,包括功能和面向OO.我也编写了一些Javascript,但从未使用(或不得不使用)多态.
现在,作为一个业余爱好项目,我想将一些用Java和C#编写的应用程序移植到Javascript中,这些应用程序大量使用了多态性.
但是乍一看我读了很多"这是可能的,但......"
那么它有替代品吗?
我想用伪写的JS写的一个例子:
abstract class Shape{ { printSurface() } ; }
class Rect : Shape() { printSurface() { print (sideA*sideB}}
class Circle : Shape() { printSurface() { print { pi*r*r }}
TheApp { myshapes.iterate(shape s) {s.printSurface() } }
Run Code Online (Sandbox Code Playgroud)
所以经典的多态案例:迭代基类.
我想实现这种行为.我知道它是多态的,但我是否有任何其他'模式'可以实现这种行为,或者我应该在Javascript中研究继承的可能性?
ZER*_*ER0 43
如上所述,JavaScript是一种基于原型继承的动态类型语言,因此您无法真正使用相同的类型语言方法.您编写的JS版本可能是:
function Shape(){
throw new Error("Abstract class")
}
Shape.prototype.printSurface = function () {
throw new Error ("Not implemented");
}
function Rect() {
// constructor;
}
Rect.prototype = Object.create(Shape.prototype);
Rect.prototype.printSurface = function() {
// Rect implementation
}
function Circle() {
// constructor;
}
Circle.prototype = Object.create(Shape.prototype);
Circle.prototype.printSurface = function() {
// Circle implementation
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的应用中:
var obj = new Circle();
if (obj instanceof Shape) {
// do something with a shape object
}
Run Code Online (Sandbox Code Playgroud)
或者,用鸭子打字:
if ("printSurface" in obj)
obj.printSurface();
// or
if (obj.printSurface)
obj.printSurface();
// or a more specific check
if (typeof obj.printSurface === "function")
obj.printSurface();
Run Code Online (Sandbox Code Playgroud)
你冷也有Shape没有任何构造函数的对象,它更像是"抽象类":
var Shape = {
printSurface : function () {
throw new Error ("Not implemented");
}
}
function Rect() {
// constructor;
}
Rect.prototype = Object.create(Shape);
Rect.prototype.printSurface = function() {
// Rect implementation
}
function Circle() {
// constructor;
}
Circle.prototype = Object.create(Shape);
Circle.prototype.printSurface = function() {
// Circle implementation
}
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,您不能再使用instanceof了,所以或者您回退到鸭子打字或者您必须使用isPrototypeOf,但仅在最近的浏览器中可用:
if (Shape.isPrototypeOf(obj)) {
// do something with obj
}
Run Code Online (Sandbox Code Playgroud)
在没有实现ES5规范的浏览器中没有Object.create,但您可以轻松使用polyfill(请参阅链接).
Wes*_*ton 13
这里的"模式"将是"界面".只要集合中的所有对象都myshapes实现了该printSurface()方法,您就可以了.
由于Javascript是动态类型语言,因此集合中的对象根本不需要相关.
我知道这可以用原型完成,但我不是使用它的大师.我更喜欢对象文字方法(更容易可视化并具有"私有"范围)
//shape class
var shape = function() {
//return an object which "shape" represents
return {
printSurface: function() {
alert('blank');
},
toInherit: function() {
alert('inherited from shape');
}
}
};
//rect class
var rect = function() {
//inherit shape
var rectObj = shape();
//private variable
var imPrivate = 'Arrr, i have been found by getter!';
//override shape's function
rectObj.printSurface = function(msg) {
alert('surface of rect is ' + msg);
}
//you can add functions too
rectObj.newfunction = function() {
alert('i was added in rect');
}
//getters and setters for private stuff work too
rectObj.newGetter = function(){
return imPrivate;
}
//return the object which represents your rectangle
return rectObj;
}
//new rectangle
var myrect = rect();
//this is the overridden function
myrect.printSurface('foo');
//the appended function
myrect.newfunction();
//inherited function
myrect.toInherit();
//testing the getter
alert(myrect.newGetter());
Run Code Online (Sandbox Code Playgroud)