JS面向对象思想(OOP)

太过爱你忘了你带给我的痛 2022-05-22 03:29 376阅读 0赞

直接看js好了,模拟创建一个奥运会

function 奥运会Class(主题) {

// 删除主题
// delete this.主题;

  1. this.主题 = 主题;
  2. this.开幕时间;
  3. this.闭幕时间;
  4. this.公告簿 = "";
  5. //模拟开幕
  6. this.开幕 = function () \{
  7. this.开幕时间 = "2012年7月28日03时12分(北京时间)";
  8. this.公告薄 = "奥运主题:" + this.主题 + "\\n" + "开幕时间:" + this.开幕时间 + "\\n" + "伦敦奥运会开幕了\\n";
  9. this.公告发布event(this, this.公告薄);
  10. \}
  11. //模拟闭幕
  12. this.闭幕 = function () \{
  13. this.闭幕时间 = "2012年8月13日 04:00(北京时间)";
  14. this.公告薄 += this.闭幕时间 + "伦敦奥运会闭幕了\\n";
  15. this.公告发布event(this, this.公告薄);
  16. \}
  17. //模拟事件的方法
  18. this.公告发布event = function(sender, e) \{
  19. \}
  20. //构建集合对象
  21. this.国家集合 = new Array();
  22. this.运动员集合 = new Array();
  23. this.裁判集合 = new Array();
  24. this.项目集合 = new Array();
  25. //创建一个通用查询
  26. this.通用查询 = function (对象源, 查询集合对象名称, 查询字段, value) \{
  27. for (var aa in 对象源) \{
  28. if (aa == 查询集合对象名称) \{
  29. var source = 对象源\[aa\];
  30. if (aa == 查询集合对象名称) \{
  31. for (var i = 0; i <= source.length; i++) \{
  32. var s = source\[i.toString()\];
  33. if (s\[查询字段\] == value) \{
  34. return s;
  35. \}
  36. \}
  37. \}
  38. \}
  39. \}
  40. return null;
  41. \}

}

//闭包扩展,在原型上扩展方法
奥运会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(项目);//添加项目到国家项目

  1. //发布消息
  2. this.公告簿 += 项目名称 + " " + "已经加入本届奥运会" + " Number:" + this.项目集合.length + "\\n";//发布消息
  3. this.公告发布event(this, this.公告簿);
  4. return this;

}

奥运会Class.prototype.添加裁判 = function(名称, 所属项目) {
var 裁判 = new 裁判Class(名称);//创建裁判
裁判.所属奥运会 = this;
裁判.所属的项目 = 所属项目;
this.裁判集合.push(裁判);//添加裁判到裁判集合

  1. //发布消息
  2. this.公告簿 += 名称 + " " + "裁判已经加入本届奥运会" + " Number:" + this.裁判集合.length + "\\n";
  3. this.公告发布event(this, this.公告簿);
  4. return this;

}

奥运会Class.prototype.添加运动员 = function(名称, 国家) {
var 运动员 = new 运动员Class(名称, 国家);//创建运动员
运动员.所属奥运会 = this;
this.运动员集合.push(运动员);//添加运动员到运动员集合
国家.添加运动员(运动员);

  1. //发布消息
  2. this.公告簿 += 名称 + " " + "加入本届奥运会" + " Number:" + this.运动员集合.length + "\\n";
  3. this.公告发布event(this, this.公告簿);
  4. return this;

}

//国家对象
function 国家Class(国家名称, 地区) {
this.名称 = 国家名称;
this.地区 = 地区;
this.所属奥运会 = null;

  1. this.运动员集合 = new Array();
  2. this.添加运动员 = function(运动员) \{
  3. this.运动员集合.push(运动员);
  4. \}

}

// 运动员对象
function 运动员Class(姓名, 所属国家) {
this.姓名 = 姓名;
this.所属奥运会 = null;
this.项目集合 = new Array();
this.所属国家 = 所属国家;

  1. this.添加项目 = function(项目) \{
  2. this.项目集合.push(项目);
  3. 项目.所属运动员集合.push(this);
  4. \}

}

//裁判对象
function 裁判Class(姓名) {
this.姓名 = 姓名;
this.所属奥运会 = null;
this.所属的项目 = null
}

//项目对象
function 项目Class(名称) {
this.名称 = 名称;
this.所属奥运会 = null;
this.成绩 = “”;
this.奖牌 = “”;
this.所属裁判 = null;

  1. this.所属运动员集合 = new Array();
  2. this.设置比赛成绩 = function() \{
  3. \}
  4. this.浅表克隆 = function() \{
  5. var clone = new 项目Class();
  6. for (var a in this) \{
  7. clone\[a\] = this\[a\];
  8. \}
  9. return clone;
  10. \}
  11. this.添加比赛成绩 = function(所属运动员, 成绩单) \{
  12. for (var i = 0; i <= this.所属运动员集合.length; i++) \{
  13. if (this.所属运动员集合\[i.toString()\].姓名 == 所属运动员.姓名) \{
  14. var s = this.所属运动员集合\[i.toString()\];
  15. for (var ii = 0; ii <= s.项目集合.length; ii++) \{
  16. if (s.项目集合\[ii.toString()\].名称 == 成绩单.名称) \{
  17. var ss = s.项目集合\[ii.toString()\];
  18. s.项目集合\[ii.toString()\] = 成绩单;
  19. this.所属奥运会.公告簿 += 所属运动员.姓名 + " " + 成绩单.名称 + " " + 成绩单.成绩 + "\\n";
  20. this.所属奥运会.公告发布event(this, this.所属奥运会.公告簿);
  21. return;
  22. \}
  23. \}
  24. \}
  25. \}
  26. \}

}

70

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读

    相关 面向对象编程思想OOP

        本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性、七大设计原则和23种设计模式之间的关系。

    相关 面向对象编程思想OOP)

    本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性、七大设计原则和23种设计模式之间的关系。 软件开