JS面向对象思想(OOP)
直接看js好了,模拟创建一个奥运会
function 奥运会Class(主题) {
// 删除主题
// delete this.主题;
this.主题 = 主题;
this.开幕时间;
this.闭幕时间;
this.公告簿 = "";
//模拟开幕
this.开幕 = function () \{
this.开幕时间 = "2012年7月28日03时12分(北京时间)";
this.公告薄 = "奥运主题:" + this.主题 + "\\n" + "开幕时间:" + this.开幕时间 + "\\n" + "伦敦奥运会开幕了\\n";
this.公告发布event(this, this.公告薄);
\}
//模拟闭幕
this.闭幕 = function () \{
this.闭幕时间 = "2012年8月13日 04:00(北京时间)";
this.公告薄 += this.闭幕时间 + "伦敦奥运会闭幕了\\n";
this.公告发布event(this, this.公告薄);
\}
//模拟事件的方法
this.公告发布event = function(sender, e) \{
\}
//构建集合对象
this.国家集合 = new Array();
this.运动员集合 = new Array();
this.裁判集合 = new Array();
this.项目集合 = new Array();
//创建一个通用查询
this.通用查询 = function (对象源, 查询集合对象名称, 查询字段, value) \{
for (var aa in 对象源) \{
if (aa == 查询集合对象名称) \{
var source = 对象源\[aa\];
if (aa == 查询集合对象名称) \{
for (var i = 0; i <= source.length; i++) \{
var s = source\[i.toString()\];
if (s\[查询字段\] == value) \{
return s;
\}
\}
\}
\}
\}
return null;
\}
}
//闭包扩展,在原型上扩展方法
奥运会Class.prototype.添加国家 = function(国家名称, 地区) {
var 国家 = new 国家Class(国家名称, 地区);//创建国家
国家.所属奥运会 = this;
this.国家集合.push(国家);//添加国家到国家集合
this.公告簿 += 国家名称 + “ “ + 地区 + “ “ + “已经加入本届奥运会\n”;;//发布消息
this.公告发布event(this, this.公告簿);
//动态移除成员 或者是方法等
// delete this.公告簿;
//链式编程,进行优化
return this;
}
奥运会Class.prototype.添加项目 = function(项目名称) {
var 项目 = new 项目Class(项目名称);//创建项目
项目.所属奥运会 = this;
this.项目集合.push(项目);//添加项目到国家项目
//发布消息
this.公告簿 += 项目名称 + " " + "已经加入本届奥运会" + " Number:" + this.项目集合.length + "\\n";//发布消息
this.公告发布event(this, this.公告簿);
return this;
}
奥运会Class.prototype.添加裁判 = function(名称, 所属项目) {
var 裁判 = new 裁判Class(名称);//创建裁判
裁判.所属奥运会 = this;
裁判.所属的项目 = 所属项目;
this.裁判集合.push(裁判);//添加裁判到裁判集合
//发布消息
this.公告簿 += 名称 + " " + "裁判已经加入本届奥运会" + " Number:" + this.裁判集合.length + "\\n";
this.公告发布event(this, this.公告簿);
return this;
}
奥运会Class.prototype.添加运动员 = function(名称, 国家) {
var 运动员 = new 运动员Class(名称, 国家);//创建运动员
运动员.所属奥运会 = this;
this.运动员集合.push(运动员);//添加运动员到运动员集合
国家.添加运动员(运动员);
//发布消息
this.公告簿 += 名称 + " " + "加入本届奥运会" + " Number:" + this.运动员集合.length + "\\n";
this.公告发布event(this, this.公告簿);
return this;
}
//国家对象
function 国家Class(国家名称, 地区) {
this.名称 = 国家名称;
this.地区 = 地区;
this.所属奥运会 = null;
this.运动员集合 = new Array();
this.添加运动员 = function(运动员) \{
this.运动员集合.push(运动员);
\}
}
// 运动员对象
function 运动员Class(姓名, 所属国家) {
this.姓名 = 姓名;
this.所属奥运会 = null;
this.项目集合 = new Array();
this.所属国家 = 所属国家;
this.添加项目 = function(项目) \{
this.项目集合.push(项目);
项目.所属运动员集合.push(this);
\}
}
//裁判对象
function 裁判Class(姓名) {
this.姓名 = 姓名;
this.所属奥运会 = null;
this.所属的项目 = null
}
//项目对象
function 项目Class(名称) {
this.名称 = 名称;
this.所属奥运会 = null;
this.成绩 = “”;
this.奖牌 = “”;
this.所属裁判 = null;
this.所属运动员集合 = new Array();
this.设置比赛成绩 = function() \{
\}
this.浅表克隆 = function() \{
var clone = new 项目Class();
for (var a in this) \{
clone\[a\] = this\[a\];
\}
return clone;
\}
this.添加比赛成绩 = function(所属运动员, 成绩单) \{
for (var i = 0; i <= this.所属运动员集合.length; i++) \{
if (this.所属运动员集合\[i.toString()\].姓名 == 所属运动员.姓名) \{
var s = this.所属运动员集合\[i.toString()\];
for (var ii = 0; ii <= s.项目集合.length; ii++) \{
if (s.项目集合\[ii.toString()\].名称 == 成绩单.名称) \{
var ss = s.项目集合\[ii.toString()\];
s.项目集合\[ii.toString()\] = 成绩单;
this.所属奥运会.公告簿 += 所属运动员.姓名 + " " + 成绩单.名称 + " " + 成绩单.成绩 + "\\n";
this.所属奥运会.公告发布event(this, this.所属奥运会.公告簿);
return;
\}
\}
\}
\}
\}
}
还没有评论,来说两句吧...