泛化(Generalization) 关联(Association) 依赖(Dependency) 聚合(Aggregation)

本文介绍了面向对象编程中类与类之间的四种基本关系:泛化、依赖、关联和聚合,并通过UML图和代码示例详细解释了每种关系的特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<clk>类与类之间的关系对于理解面向对象具有很重要的作用,以前在<nobr onmousemove="kwM(3);" onmouseout="kwL(event,this)" onmouseover="kwE(event,3, this);" oncontextmenu="return false;" target="_blank" onclick="return kwC(event,3)" style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" id="clickeyekey3">面试</nobr>的时候也经常被问到这个问题,在这里我就介绍一下。</clk> 类与类之间存在以下关系: (1)泛化(Generalization) (2)关联(Association) (3)依赖(Dependency) (4)聚合(Aggregation)

UML图与应用代码例子: 1.泛化(Generalization) [泛化]新新乐园 表示类与类之间的继承关系,接口与接口之间的继承关系,或类对接口的实现关系。一般化的关系是从子类指向父类的,与继承或实现的方法相反。 [具体表现] 父类父类实例=new子类() [UML图](图1.1) 图1.1Animal类与Tiger类,Dog类的依赖关系 [代码表现]

  1. classAnimal{}
  2. classTigerextendsAnimal{}
  3. publicclassTest
  4. {
  5. publicvoidtest()
  6. {
  7. Animala=newTiger();
  8. }
  9. }

2.依赖(Dependency) [依赖] <clk>对于两个相对独立的对象,当一个对象负责构造另一个对象的实例,或者依赖另一个对象的<nobr onmousemove="kwM(1);" onmouseout="kwL(event,this)" onmouseover="kwE(event,1, this);" oncontextmenu="return false;" target="_blank" onclick="return kwC(event,1)" style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" id="clickeyekey1">服务</nobr>时,这两个对象之间主要体现为依赖关系。</clk>[具体表现] 依赖关系表现在局部变量方法的参数,以及对静态方法的调用 [现实例子] 比如说你要去拧螺丝,你是不是要借助(也就是依赖)螺丝刀(Screwdriver)来帮助你完成拧螺丝(screw)的工作 [UML表现](图1.2)

图1.2Person类与Screwdriver类的依赖关系 [代码表现]

publicclassPerson{ /**拧螺丝*/ publicvoidscrew(Screwdriverscrewdriver){ screwdriver.screw(); } }

.关联(Association) [关联] <clk>对于两个相对独立的对象,当一个对象的实例与另一个对象的一些特定实例存在固定的<nobr onmousemove="kwM(6);" onmouseout="kwL(event,this)" onmouseover="kwE(event,6, this);" oncontextmenu="return false;" target="_blank" onclick="return kwC(event,6)" style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" id="clickeyekey6">对应</nobr>关系时,这两个对象之间为关联关系。</clk>[具体表现] 关联关系是使用实例变量来实现 [现实例子] 比如客户<clk>和订单,每个订单对应特定的客户,每个客户对应一些特定的订单;再例如<nobr onmousemove="kwM(0);" onmouseout="kwL(event,this)" onmouseover="kwE(event,0, this);" oncontextmenu="return false;" target="_blank" onclick="return kwC(event,0)" style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" id="clickeyekey0">公司</nobr>和员工,每个公司对应一些特定的员工,每个员工对应一特定的公司</clk>[UML图] (图1.3) 图1.3公司和员工的关联关系 [代码表现]

publicclassCompany{ privateEmployeeemployee; publicEmployeegetEmployee(){ returnemployee; } publicvoidsetEmployee(Employeeemployee){ this.employee=employee; } //公司运作 publicvoidrun(){ employee.startWorking(); } }

(4)聚合(Aggregation) [聚合] 当对象A被加入到对象B中,成为对象B的组成部分时,对象B和对象A之间为聚集关系。聚合是关联关系的一种,是较强的关联关系,强调的是整体部分之间的关系。新 [具体表现] 与关联关系一样,聚合关系也是通过实例变量来实现这样关系的。关联关系和聚合关系来语法上是没办法区分的,从语义上才能更好的区分两者的区别。 [关联与聚合的区别] <clk>(1)关联关系所涉及的两个对象是处在同一个<nobr onmousemove="kwM(2);" onmouseout="kwL(event,this)" onmouseover="kwE(event,2, this);" oncontextmenu="return false;" target="_blank" onclick="return kwC(event,2)" style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" id="clickeyekey2">层次</nobr>上的。比如人和自行车就是一种关联关系,而不是聚合关系,因为人不是由自行车组成的。</clk><clk>聚合关系涉及的两个对象处于不平等的层次上,一个代表整体,一个代表部分。比如<nobr onmousemove="kwM(4);" onmouseout="kwL(event,this)" onmouseover="kwE(event,4, this);" oncontextmenu="return false;" target="_blank" onclick="return kwC(event,4)" style="border-bottom: 1px dotted rgb(102, 0, 255); text-decoration: underline; color: rgb(102, 0, 255); background-color: transparent;" id="clickeyekey4">电脑</nobr>和它的显示器、键盘、主板以及内存就是聚集关系,因为主板是电脑的组成部分。</clk> (2) 对于具有聚集关系(尤其是强聚集关系)的两个对象,整体对象会制约它的组成对象的生命周期。部分类的对象不能单独存在,它的生命周期依赖于整体类的对象的 生命周期,当整体消失,部分也就随之消失。比如张三的电脑被偷了,那么电脑的所有组件也不存在了,除非张三事先把一些电脑的组件(比如硬盘和内存)拆了下 来。 [UML图](图1.4) 图1.3电脑和组件的聚合关系 [代码表现]

publicclassComputer{ privateCPUcpu; publicCPUgetCPU(){ returncpu; } publicvoidsetCPU(CPUcpu){ this.cpu=cpu; } //开启电脑 publicvoidstart(){ //cpu运作 cpu.run(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值