父对象调用子方法
MooTools: Parent object calls child method
我正在启动一个项目,我将主要为web应用程序开发前端GUI,并决定使用MooTools而不是jQuery,因为它具有更好的面向对象功能。然而,在测试过程中,从我作为Java开发人员的角度来看,我遇到了一些奇怪的事情。问题在这里:
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!
现在我有点困惑了…最后一行不应该是"来自父母!"吗?
不,这就是多态性应该如何工作的。即使调用在Parent类中定义的方法,您仍然处于Child 实例中,因此调用Child中被覆盖的方法。
相关文章:
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- javascript从子方法调用父方法
- 我应该返回一个类似console.log()的方法调用吗
- NodeJS-从同一文件中的另一个方法调用一个方法
- 使用restangular save方法调用put与post时的控件
- 从实例方法调用实例方法
- Node.js-异步方法调用问题
- JS对象文字中的方法和子方法
- 如何在 javascript 中从子类调用父方法
- 子对象调用兄弟姐妹'方法
- 当方法不属于父级的原型时,有没有办法从子级调用父级的方法
- 如何从子方法调用父方法
- 如何在 javascript 中从子原型调用父类的构造函数方法
- 我如何从父调用原型子方法
- 在Javascript中从子方法调用父方法
- 在JavaScript中从父方法调用子方法
- jQtree,调用嵌套列表中的方法调用父方法和子方法
- 父对象调用子方法
- 如何从子组件调用父组件中的方法
- Javascript - 从子方法继承和调用父方法