码迷,mamicode.com
首页 > 其他好文 > 详细

OC 方法和函数

时间:2015-12-09 23:17:47      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

 

 

/*

 方法

 1.对象方法都是以减号 - 

 2.对象方法的声明必须写在@interface和@end之间

   对象方法的实现必须写在@implementation和@end之间

 3.对象方法只能由对象来调用

 4.对象方法归类\对象所有

 

 函数

 1.函数能写在文件中的任意位置(@interface和@end之间除外),函数归文件所有

 2.函数调用不依赖于对象

 3.函数内部不能直接通过成员变量名访问某个对象的成员变量

 

 */

 

#import <Foundation/Foundation.h>

 

@interface Person : NSObject

@end

 

@implementation Person

@end

 

@interface Car : NSObject

{// 成员变量\实例变量

    //int wheels = 4; 不允许在这里初始化

    //static int wheels; 不能随便将成员变量当做C语言中的变量来使用

    @public

    int wheels;

}

 

- (void)run;

- (void)fly;

@end

 

int main()

{

    // wheels = 10;

    /*

    Car *c = [Car new];

    c->wheels = 4;

    //run();

    

    [c run];

    */

    

    void test2();

    

    test2();

    

    return 0;

}

 

@implementation Car

 

- (void) fly

{

    

}

 

/*

void test2()

{

    NSLog(@"调用了test2函数-%d", wheels);

}*/

 

void test()

{

    NSLog(@"调用了test函数");

}

 

- (void)run

{

    test();

    NSLog(@"%d个轮子的车跑起来了", wheels);

}

@end

OC 方法和函数

标签:

原文地址:http://www.cnblogs.com/oc-bowen/p/5034381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!