首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

javascript面向对象编程-种的定义

2013-08-10 
javascript面向对象编程----类的定义window.onload function() {var someone new Person(someone)v

javascript面向对象编程----类的定义
window.onload = function() {var someone = new Person("someone");var sb = new Person("sb", 99);someone.setName("wod").setAge(100).showInfo();// alert(typeof Person);//function// alert(typeof someone);//object};/******************************************************************************* * class Person ******************************************************************************/var Person = function(name, age) {if (name == undefined)throw new Error("构造器需要 name参数");this.setName(name);this.setAge(age);};Person.prototype = {getName : function() {return this.name;},setName : function(name) {this.name = name;return this;// 能够链式调用},getAge : function() {return this.age;},setAge : function(age) {if (age < 0 || age > 120)throw new Error("invalid Person age");// 简单checkthis.age = age || 20;// 构造器无age参数时,默认值20return this;},showInfo : function() {return this.name + "'s" + " age is " + this.age + " years old.";}};

?

?

?

用闭包实现带有私有成员的类

?

window.onload = function() {var someone = new Person("t");var sb = new Person("sb", 99);var bbb = someone.setAge(12).showInfo();alert(bbb)};/******************************************************************************* * class Person ******************************************************************************/var Person = function(newName, newAge) {if (newName == undefined)throw new Error("构造器需要 name参数");// -------------------private start-------------------// 私有属性var name, age;// 私有方法function checkAge(age) {if (age < 0 || age > 120) {throw new Error("invalid Person age");// 简单checkreturn false;}return true;}// -------------------private end -------------------// -------------------public start-------------------// 特权方法(privileged methods),能够访问私有属性,需要访问私有属性的方法才设成特权方法this.getName = function() {return name;};this.setName = function(newName) {name = newName;return this;// 能够链式调用};this.getAge = function() {return age;};this.setAge = function(newAge) {checkAge(newAge)age = newAge || 20;// 构造器无age参数时,默认值20return this;};this.showInfo = function() {return name + "'s" + " age is " + age + " years old.";};//// 构造器this.setName(newName);this.setAge(newAge);};// 公开的不需访问私有属性的方法定义Person.prototype = {method1 : function() {// ...},};// -------------------public end-------------------

?

?

?静态方法的定义

?

window.onload = function() {Person.staticMethod1();//静态调用Person.showPersonNum();};/******************************************************************************* * class Person ******************************************************************************/var Person = (function() {// -------------------private start-------------------// private static attributes 私有静态属性var numOfPersons = 0;var numOfFingers = 10;// 私有静态方法function showFingerNum() {return numOfFingers;}var ctor = function(newName, newAge) {if (newName == undefined)throw new Error("构造器需要 name参数 constructor need a name");// 私有属性var name, age;// 私有方法(与定义在return外的私有静态方法用法上一样)function checkAge(age) {if (age < 0 || age > 120) {throw new Error("invalid Person age");// 简单checkreturn false;}return true;}// -------------------private end -------------------// -------------------public start-------------------// 特权方法(privileged methods),能够访问私有属性,需要访问私有属性的方法才设成特权方法this.getName = function() {return name;};this.setName = function(newName) {name = newName;return this;// 能够链式调用};this.getAge = function() {return age;};this.setAge = function(newAge) {checkAge(newAge);age = newAge || 20;// 构造器无age参数时,默认值20return this;};this.showInfo = function() {return name + "'s" + " age is " + age + " years old."+ showFingerNum();};// 构造器numOfPersons++;this.setName(newName);this.setAge(newAge);};// public privileged static method 能够访问私有属性的静态方法;ctor.showPersonNum = function() {alert("there has "+numOfPersons+" Person been created");};return ctor;})();// public non-privileged static method 公开的不需访问私有属性的静态方法定义Person.staticMethod1 = function() {alert("Person.staticMethod1");};// public, non-privileged methods 公开的不需访问私有属性的方法定义Person.prototype = {method1 : function() {alert("method1");},method2 : function() {alert("method2");}};// -------------------public end-------------------
?

热点排行