Ext JS 继承

来源:互联网 发布:网络电视直播pc版 编辑:程序博客网 时间:2024/05/29 18:11

继承

Ext JS也拥有“面向对象”的概念:封装、继承和多态等。当定义一个新的类的的时候,可以使用extend这个关键词来从已知的类继承。

先定义一个父类,其中有个getName的方法:

Ext.define('Person', {    name : 'Unknown',    constructor : function(name){        if(name){            this.name = name;        }    },    getName : function(){        alert("My name is " + this.name);    }});

紧接着,定义一个子类:

Ext.define('Student', {    extend : 'Person',    schoolName : 'Unknown',    constructor : function(name, schoolName){        this.schoolName = schoolName || 'Unknown';        //call parent class constructor        this.callParent(arguments);    },    getSchoolName : function(){        alert("My school name is " + this.schoolName);    }});var newStudent = new Student('XYZ', 'ABC School');newStudent.getName(); //output: XYZnewStudent.getSchoolName(); //output: ABC School

像上面这样,Student类使用extend关键词来继承Person类,当我们在尝试调用子类的getName()的方法时,实际上调用的是父类的方法。

原创粉丝点击