• ObjectiveC 组合


    #import <Foundation/Foundation.h>
    
    
    // --------------------------------------------------
    
    @interface Tire : NSObject
    @end // Tire
    
    
    @implementation Tire
    
    - (NSString *) description
    {
        return (@"I am a tire. I last a while");
    } // description
    
    @end // Tire
    
    
    
    // --------------------------------------------------
    
    @interface Engine : NSObject
    @end // Engine
    
    
    @implementation Engine
    
    - (NSString *) description
    {
        return (@"I am an engine.  Vrooom!");
    } // description
    
    @end // Engine
    
    
    // --------------------------------------------------
    
    @interface Car : NSObject
    {
        Engine *engine;
        Tire *tires[4];
    }
    
    - (void) print;
    
    @end // Car
    
    
    @implementation Car
    
    - (id) init
    {
        if (self = [super init]) {
            engine = [Engine new];
            
            tires[0] = [Tire new];
            tires[1] = [Tire new];
            tires[2] = [Tire new];
            tires[3] = [Tire new];
        }
        
        return (self);
        
    } // init
    
    - (void) print
    {
        NSLog (@"%@", engine);
        
        NSLog (@"%@", tires[0]);
        NSLog (@"%@", tires[1]);
        NSLog (@"%@", tires[2]);
        NSLog (@"%@", tires[3]);
        
    } // print
    
    @end // Car
    
    
    // --------------------------------------------------
    
    int main (int argc, const char * argv[]) 
    {
        Car *car;
        
        car = [Car new];
        [car print];
        
        return (0);
        
    } // main


    运行结果:

    I am an engine. Vrooom!
    I am a tire. I last a while.
    I am a tire. I last a while.
    I am a tire. I last a while.
    I am a tire. I last a while.

    技术改变世界
  • 相关阅读:
    php实现rpc简单的方法
    统计代码量
    laravel的速查表
    header的参数不能带下划线
    PHP简单实现单点登录功能示例
    phpStorm函数注释的设置
    数据结构基础
    laravel生命周期和核心思想
    深入理解php底层:php生命周期
    Jmeter:实例(性能测试目标)
  • 原文地址:https://www.cnblogs.com/davidgu/p/2995810.html
Copyright © 2020-2023  润新知