1. 程式人生 > >OC方法和函式的區別

OC方法和函式的區別

  oc方法和函式的區別

/*

 1> 方法都是以減號開頭 -

 2> 方法的宣告必須寫在@interface @end之間

    物件方法的實現必須寫在@implementation @end之間

 3> 物件方法只能由物件來呼叫

 4> 方法歸 類/物件 所有

 函式能寫在檔案的任何位置(@interface @end 之間除外)

函式歸檔案所有

函式呼叫不依賴物件

函式內部不能直接通過成員變數名訪問某個物件的成員變數

 */

#import <Foundation/Foundation.h>

@interface Car :

NSObject

{

@public

   int wheels;

   int speed;

}

-(void)run;

@end

@implementation Car

-(void)run

{

NSLog(@"%d個輪子,速度為%dkm/h",wheels,speed);

}

@end

<#methods#>

@end

int main(int argc,const char * argv[]) {

@autoreleasepool {

        Car *p = [Car new];

        NSLog(@"Hello, World!"

);

    }

   return 0;

}