网络编程
位置:首页>> 网络编程>> JavaScript>> javascript中类的创建和继承(2)

javascript中类的创建和继承(2)

作者:yring 来源:经典论坛 发布时间:2008-05-08 12:07:00 

标签:类,prototype,继承,javascript

prototype.js中的类定义和继承

相关推荐:

prototype.js1.4版开发者手册

prototype-1.4.0注释版源代码下载

1.6.0以前:

/** obsolete syntax **/    
var Person = Class.create();    //通过Class.create方法创建空类   
Person.prototype = {               //把方法定义到prototype中,注意,是通过initalize方法初始化类的属性   
  initialize: function(name) {    
    this.name = name;    
  },    
  say: function(message) {    
    return this.name + ': ' + message;    
  }    
};    
  
var guy = new Person('Miro');    
guy.say('hi');    
// -> "Miro: hi"    
                                            //prototype中的继承方式:   
var Pirate = Class.create();    //建立空类;   
// inherit from Person class:    
Pirate.prototype = Object.extend(new Person(), {    //先实例化超类,再把超类中的方法复制到子类中去,   
  // redefine the speak method                               //注意,实际上prototype类定义机制中并没有直接定义   
say: function(message) {                                       //类的属性而是通过intilize方法,而且所有的方法都   
    return this.name + ': ' + message + ', yarr!';      //之直接定义在prototype中,所以直接用原型链方式   
  }                                                                        //继承超类的所有方法不会产生问题。   
});    
  
var john = new Pirate('Long John');    
john.say('ahoy matey');    
// -> "Long John: ahoy matey, yarr!"  
/** obsolete syntax **/ 
var Person = Class.create();    //通过Class.create方法创建空类
Person.prototype = {               //把方法定义到prototype中,注意,是通过initalize方法初始化类的属性
  initialize: function(name) { 
    this.name = name; 
  }, 
  say: function(message) { 
    return this.name + ': ' + message; 
  } 
}; 
var guy = new Person('Miro'); 
guy.say('hi'); 
// -> "Miro: hi" 
                                            //prototype中的继承方式:
var Pirate = Class.create();    //建立空类;
// inherit from Person class: 
Pirate.prototype = Object.extend(new Person(), {    //先实例化超类,再把超类中的方法复制到子类中去,
  // redefine the speak method                               //注意,实际上prototype类定义机制中并没有直接定义
say: function(message) {                                       //类的属性而是通过intilize方法,而且所有的方法都
    return this.name + ': ' + message + ', yarr!';      //之直接定义在prototype中,所以直接用原型链方式
  }                                                                        //继承超类的所有方法不会产生问题。
}); 
var john = new Pirate('Long John'); 
john.say('ahoy matey'); 
// -> "Long John: ahoy matey, yarr!"

来看一下Class.create方法的实现代码

var Class = {   
  create: function() {   
    return function() {                                          //实际上把所有的属性定义到intiliaze方法(实际上是一个类)中,   
      this.initialize.apply(this, arguments);              //然后通过对象冒充方式继承该类   
    }   
  }   
}             
var Class = {
  create: function() {
    return function() {                                          //实际上把所有的属性定义到intiliaze方法(实际上是一个类)中,
      this.initialize.apply(this, arguments);              //然后通过对象冒充方式继承该类
    }
  }
}    

可以从prototype的例子充分体会到通过对象冒充和原型链类继承的差别,一般来说属性需用对象冒充方式继承,方法需用原型链方式继承。

prototype-1.6.0以后版本:

Java代码

1.6.0以后,对prototype的类进行了更多的扩展,举例:

/** new, preferred syntax **/    
// properties are directly passed to `create` method    
var Person = Class.create({    
  initialize: function(name) {                       //不必定义一个空类,and定义方法的位置改变   
    this.name = name;    
  },    
  say: function(message) {    
    return this.name + ': ' + message;    
  }    
});    
  
// when subclassing, specify the class you want to inherit from    
var Pirate = Class.create(Person, {            //第一个参数是class,作为超类在定义类时直接继承   
  // redefine the speak method    
say: function($super, message) {    
    return $super(message) + ', yarr!';    
  }    
});    
  
var john = new Pirate('Long John');    
john.say('ahoy matey');    
// -> "Long John: ahoy matey, yarr!"   

1.6.0以后,对prototype的类进行了更多的扩展,举例:

/** new, preferred syntax **/ 
// properties are directly passed to `create` method 
var Person = Class.create({ 
  initialize: function(name) {                       //不必定义一个空类,and定义方法的位置改变
    this.name = name; 
  }, 
  say: function(message) { 
    return this.name + ': ' + message; 
  } 
}); 
// when subclassing, specify the class you want to inherit from 
var Pirate = Class.create(Person, {            //第一个参数是class,作为超类在定义类时直接继承
  // redefine the speak method 
say: function($super, message) { 
    return $super(message) + ', yarr!'; 
  } 
}); 
var john = new Pirate('Long John'); 
john.say('ahoy matey'); 
// -> "Long John: ahoy matey, yarr!"

实现的代码比较复杂,但是基本原理和1.60以前版本差不多,也是用对象冒充继承initialize类,用原型链继承超类的方法,不过中间定义了一个klass对象,类似于工厂函数的方式

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com