理解objective-c中static

理解static

 

Test.h文件

 

//  Test.h

//  Twopage

//

//  Created by Evis on 12-7-28.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface Test : NSObject{

}

 

+(int) staticIntY;

 

@end

 

Test.m文件

 

//  Test.m

//  Twopage

//

//  Created by Evis on 12-7-28.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

 

#import “Test.h”

 

static int intY=10;

 

@implementation Test

 

+(int) staticIntY{

    intY+=1;

    return intY;

}

 

@end

 

main.m文件

 

//  main.m

//  Twopage

//

//  Created by Evis on 12-7-16.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import “Test.h”

 

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

{

 

    @autoreleasepool {

       

        // insert code here…

        NSLog(@”%i”,[Test staticIntY]);

        NSLog(@”%i”,[Test staticIntY]);

    }

    return 0;

}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注