我正在启动一个项目,主要为 Web 应用程序开发前端 GUI,并决定使用 MooTools 而不是 jQuery,因为它具有更好的 OOP 功能。然而,在测试时,从我作为 Java 开发人员的 Angular 来看,我遇到了一些奇怪的事情。问题是这样的:
var Parent = new Class({
initialize: function() {
console.log("Parent constructor call!");
},
show: function() {
console.log("From Parent!");
},
someParentMethod: function() {
console.log("Some parent method");
this.show();
}
});
var Child = new Class({
Implements: Parent,
initialize: function() {
console.log("Child constructor call!");
},
show: function() {
console.log("From Child!");
},
display: function() {
this.show();
this.someParentMethod();
}
});
var c = new Child();
c.display();
其输出如下:
Parent constructor call!
Child constructor call!
From Child!
Some parent method
From Child!
现在我有点困惑......最后一行不应该是“From Parent!”吗?
最佳答案
不,这就是多态性应该如何工作的。即使您正在调用 Parent 类中定义的方法,您仍然处于 Child 实例中,因此将调用 Child 中的重写方法。
关于javascript - MooTools:父对象调用子方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15202188/