首页 > 代码库 > Objective-C的Public, Protected, Private变量和方法的使用
Objective-C的Public, Protected, Private变量和方法的使用
.h文件
#import <Foundation/Foundation.h>
@interface Grammar : NSObject {
NSString* protectedVariable1;
@public
NSString* publicVariable1;
@protected
NSString* protectedVariable2;
@private
NSString* privateVariable;
}
NSString* staticVariable;
@property (nonatomic, retain) NSString* publicVariable2;
+ (void)staticMethod;
- (void)publicMethod;
@end
.m文件
#import "Grammar.h"
//私有方法以category方式实现
#pragma mark -
#pragma mark Grammar(private)
@interface Grammar(private)
- (void)privateMethod;
@end
#pragma mark -
#pragma mark Grammar
@implementation Grammar
@synthesize publicVariable2;
#pragma mark -
#pragma mark Public Method
+ (void)staticMethod
{
}
- (void)publicMethod
{
}
#pragma mark -
#pragma mark Private Method
- (void)privateMethod
{
}
@end
Objective-C的Public, Protected, Private变量和方法的使用