【《Objective-C基础教程 》笔记ch05】(六)OC中的复合机制Composition

 1、复合通过包括作为实例变量的的对象指针实现的。

       @interface Unicycle : NSObject

       {

          Pedal*pedal;

          Tire*tire;

        }//Pedal和tire通过复合的方式组成了Unicycle


2、存取方法——用来读取或者改变某个对象属性的方法。


#import <Foundation/Foundation.h>


@interface Car : NSObject

{

    Engine *engine;

    Tire * tires[4];

}


- (Engine *) engine;//getter訪问器

- (void) setEngine : (Engine *)newEngine;//setter訪问器

- (Tire *) tireAtIndex : (int)index;

- (void) setTire : (Tire *)tire atIndex : (int)Index;


@end//Car


3、继承与复合

     继承是“is A”关系;复合是“has A”关系。


原文地址:https://www.cnblogs.com/yxwkf/p/3837647.html