介绍在ExtJS 4中类的继承。

几点说明


  1. 这边文章主要讲解如何去扩展一个已经存在的类,以及使用新的方法重载父类的方法。
  2. 我们将定义一个简单的类Vehicle,在其中有Manufacturer、Model、Top speed三个属性,一个travel方法。

 

代码


ExtJS:4.0.7

IDE:Eclipse Java EE

浏览器:Chrome 25

//-------父类--------------------------------------
   2:  
'Cookbook.Vehicle', {
   4:     config : {
'Unknown Manufacturer',
'Unknown Model',
   7:         topSpeed : 0
   8:     },
   9:  
function(manufacturer, model, topSpeed) {
// initialise our config object
this.initConfig();
  13:  
if (manufacturer) {
this.setManufacturer(manufacturer);
  16:         }
if (model) {
this.setModel(model);
  19:         }
if (topSpeed) {
this.setTopSpeed(topSpeed);
  22:         }
  23:     },
  24:  
function(distance) {
this.getModel()
this.getTopSpeed()
'mph');
  29:     }
function() {
'Vehicle Class defined!');
  32: });
  33:  
//-------调用父类------------------------------------
  35:  
'Vanquish', 60);
  37: vehicle.travel(100);
  38:  
//-------子类--------------------------------------
  40:  
'Cookbook.Plane', {
'Cookbook.Vehicle',
  43:  
  44:     config : {
  45:         maxAltitude : 0
  46:     },
  47:  
function(manufacturer, model, topSpeed, maxAltitude) {
// intialise our config object
this.initConfig();
  51:  
if (maxAltitude) {
this.setMaxAltitude(maxAltitude);
  54:         }
  55:  
// call the parent class' constructor
this.callParent([ manufacturer, model, topSpeed ]);
  58:     }
function() {
'Plane Class Defined!');
  61: });
  62:  
//-------实例化并调用----------------------------------
  64:  
'747',500,30000);
  66: plane.travel(800);

相关文章: