且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

“@private"是什么意思?在Objective-C中是什么意思?

更新时间:2022-06-22 02:02:31

它是一个可见性修饰符——这意味着声明为 @private 的实例变量只能通过同一类的实例.子类或其他类无法访问私有成员.

It's a visibility modifier—it means that instance variables declared as @private can only be accessed by instances of the same class. Private members cannot be accessed by subclasses or other classes.

例如:

@interface MyClass : NSObject
{
    @private
    int someVar;  // Can only be accessed by instances of MyClass

    @public
    int aPublicVar;  // Can be accessed by any object
}
@end

另外,澄清一下,Objective-C 中的方法总是公开的.不过,有多种方法可以隐藏"方法声明——参见 这个问题 了解更多信息.

Also, to clarify, methods are always public in Objective-C. There are ways of "hiding" method declarations, though—see this question for more information.