UML中的关联关系

UML中的关联关系其内在意思就是has a 如图: 

 

相对于依赖关系,关联关系在代码中有所体现.上图中的关联关系在代码中体现为 
 

     

其中water 中将Climate作为其中的属性. 
当然,关联关系中也有双相关联,如图:

 

关联又分为组合,聚合

 

对应的代码如下: 

 设计模式中的关联关系 

 

代码如下:

  1: //工作经历
  2:     class WorkExperience
  3:     {
  4:         private string workDate;
  5:         public string WorkDate
  6:         {
  7:             get { return workDate; }
  8:             set { workDate = value; }
  9: 
 10:         }
 11: 
 12:         private string company;
 13:         public string Company
 14:         {
 15:             get { return workDate; }
 16:             set { workDate = value; }
 17:         }
 18:     }
 19: 
 20:     //简历
 21:     class Resume : ICloneable
 22:     {
 23:         private string name;
 24:         private string sex;
 25:         private string age;
 26:         
 27:         private WorkExperience work;//组合关系,简历必须有工作经历
 28: 
 29:         public Resume(string name)
 30:         {
 31:             this.name = name;
 32:             work = new WorkExperience();
 33:         }
 34: 
 35:         //设置个人信息
 36:         public void SetPersonInfo(string sex, string age)
 37:         {
 38:             this.age = age;
 39:             this.sex = sex;
 40:         }
 41: 
 42:         //设置个人经历
 43:         public void SetWorkExperience(string workDate, string company)
 44:         {
 45:             work.WorkDate = workDate;
 46:             work.Company = company;
 47:         }
 48:         //显示
 49:         public void Display()
 50:         {
 51:             Console.WriteLine("个人信息");
 52: 
 53:         }
 54:         public object clone()
 55:         {
 56:             return (Object)this.MemberwiseClone();
 57: 
 58:         }
 59:     }

 转自:http://blog.csdn.net/lsh6688/article/details/6027922

posted on 2016-12-02 15:43  苦涩的茶  阅读(709)  评论(0编辑  收藏  举报

// 1.生成目录索引列表