ojective-C学习笔记(6)复合

通过之前的学习,知道了继承是在两个类之间建立关系的一种方式,它可以避免许多重复的代码。

使用复合可以组合多个对象,让它们分工协作。事实上,我们经常同时使用继承和复合来创建自己的类,掌握两个概念十分重要。

下面通过一个小程序来了解复合是什么。

//Tire类
@interface Tire : NSObject

@end

@implementation Tire

- (NSString *)description
{
    return [NSString stringWithFormat:@"I am a tire."];
}

@end

//Engien类
@interface Engine : NSObject

@end

@implementation Engine

- (NSString *)description
{
    return [NSString stringWithFormat:@"I am an engien."];
}

@end

//Car类,一辆汽车有一个引擎和4个轮子。
@interface Car : NSObject{
    Engine *engien;
    Tire *tires[4];
}

- (void)print;

@end

@implementation Car

- (instancetype) init{
    if(self = [super init]){
        engien = [Engine new];
        tires[0] = [Tire new];
        tires[1] = [Tire new];
        tires[2] = [Tire new];
        tires[3] = [Tire new];
    }
    return self;
}

- (void)print{
    NSLog(@"%@", engien);
    NSLog(@"%@", tires[0]);
    NSLog(@"%@", tires[1]);
    NSLog(@"%@", tires[2]);
    NSLog(@"%@", tires[3]);
}

@end

//main函数

int main(int argc, const char * argv[]) {
    Car *car = [[Car alloc] init];
    [car print];
    
    return 0;
}

输出结果为:

2018-04-21 20:00:26.610274+0800 Car[5750:735664] I am an engien.
2018-04-21 20:00:26.610512+0800 Car[5750:735664] I am a tire.
2018-04-21 20:00:26.610535+0800 Car[5750:735664] I am a tire.
2018-04-21 20:00:26.610548+0800 Car[5750:735664] I am a tire.
2018-04-21 20:00:26.610558+0800 Car[5750:735664] I am a tire.

猜你喜欢

转载自www.cnblogs.com/ccyag/p/8903490.html