声明私有成员变量

发布于 2024-09-16 15:20:42 字数 286 浏览 9 评论 0原文

我几周前开始学习 Objective-C,但我仍然不明白如何正确管理类的封装。在类中声明私有成员变量的最佳方法是什么?

看来,使用“@property”为成员变量设置正确的 getter/setter 是正确的方法,而不仅仅是在界面中声明它“@private”。但在我看来,这仍然让其他类可以访问这些变量。即使您将属性声明为“只读”,外部类也可以访问对成员变量的引用并修改它!

所以我猜测声明私有成员变量的最佳方法是通过不声明属性来不包含任何 getter/setter。我说得对吗?或者有更好的方法吗?

谢谢

I've started learning Objective-C a few weeks ago and I still don't understand how to manage the encapsulation of a class correctly. What is the best way to declare a private member variable in a class?

It seems that setting the right getter/setter for your member variable with "@property" is the right way to go, more than just declaring it "@private" in the interface. But it seems to me that this still gives other classes an access to these variables. Even if you declare the property "readonly", an outside class can access the reference to the member variable and modify it!

So I'm guessing the best way to declare a private member variable is to not include any guetter/setter by not declaring a property. Am i right? Or is there a better way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‘画卷フ 2024-09-23 15:20:42

如果您不希望其他类可以访问它,请在您的实现上声明 @property,为您的类创建一个匿名类别。

头文件:

// MyClass.h
@interface MyClass : NSObject {
    NSObject *_privateObject;
    NSObject *_readonlyObject;
    NSObject *_publicObject;
}

@property (nonatomic, retain, readonly) NSObject *readonlyObject;
@property (nonatomic, retain) NSObject *publicObject;

@end

实现:

// MyClass.m
@interface MyClass ()
    @property (nonatomic, retain) NSObject *privateObject;
    // Make it writable on the implementation
    @property (nonatomic, retain, readwrite) NSObject *readonlyObject;
@end

@implementation MyClass

@synthesize privateObject = _privateObject;
@synthesize readonlyObject = _readonlyObject;
@synthesize publicObject = _publicObject;

这些是三个不同属性的示例。

  • privateObject 在其他类上不可见;
  • readonlyObject 可见但只读;
  • publicObject 可见,可以获取和设置;

if you don't want it accessible to other classes, declare the @property on your implementation, creating an anonymous category for your class.

Header file:

// MyClass.h
@interface MyClass : NSObject {
    NSObject *_privateObject;
    NSObject *_readonlyObject;
    NSObject *_publicObject;
}

@property (nonatomic, retain, readonly) NSObject *readonlyObject;
@property (nonatomic, retain) NSObject *publicObject;

@end

Implementation:

// MyClass.m
@interface MyClass ()
    @property (nonatomic, retain) NSObject *privateObject;
    // Make it writable on the implementation
    @property (nonatomic, retain, readwrite) NSObject *readonlyObject;
@end

@implementation MyClass

@synthesize privateObject = _privateObject;
@synthesize readonlyObject = _readonlyObject;
@synthesize publicObject = _publicObject;

These are examples of three different properties.

  • privateObject is not visible on other classes;
  • readonlyObject is visible but is read only;
  • publicObject is visible and can be get and set;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文