zoukankan      html  css  js  c++  java
  • 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.

    技术改变世界
  • 相关阅读:
    开启mysql的远程访问权限
    react生命周期
    代码分析工具-SonarQube的安装及使用
    数据源连接神器-DBeaver
    内网搭建pip镜像源
    MySQL5.6源码包安装
    Oracle11g 离线静默安装并附安装脚本
    如何上手DataX
    RockeMQ集群部署
    Redis集群搭建
  • 原文地址:https://www.cnblogs.com/davidgu/p/2995810.html
Copyright © 2011-2022 走看看