Object-C 中私有变量和私有方法

1、在OC中有私有变量和私有方法,声明变量的时候 使用 @private 关键字 仍然可以使用指向运算符来 查看,如下:

@interface Clip : NSObject
{
@private
    int _cbullet;
    int _times;
}

在main.m 中仍可以使用p->_cbullet; 来查看,但是无法访问。

在OC中 @interface和@implementation 都可以定义实例变量。

#import "Clip.h"

@implementation Clip
{
    int _num;
}
@end

在@implementation中定义的实例变量(成员变量)默认就是@private 变量,但在main.m 中就无法使用指向运算符来查看,也无法访问,xcode无法联想出 _num 变量。@implementation定义的变量只能在本类中使用,无法在其它类和子类中使用。

#import "Clip.h"

@implementation Clip
{
    int _num;
}
- (void) test
{
     NSlog(@"%i",_num);
}
@end

在@implementation中 使用@public 修饰 int _num; ,在main.m中仍然无法查看。

2、私有方法 

  在@interface中没有声明,仅在@implementation 中实现的方法就称之为私有方法,其它类不能够直接访问,[p test] 不能直接访问。

 

posted @ 2016-03-30 15:51  古风云飞扬  阅读(482)  评论(0)    收藏  举报