• 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.

    技术改变世界
  • 相关阅读:
    ARTS习惯(8)
    ARTS习惯(7)
    ARTS习惯(6)
    ARTS习惯(5)
    ARTS习惯(4)
    ARTS习惯(3)
    线程状态
    java线程同步
    数据库视图
    数据库事务
  • 原文地址:https://www.cnblogs.com/davidgu/p/2995810.html
Copyright © 2020-2023  润新知