@private是如何实现的?
在 Objective-C 中,我很好奇如何实现实例变量的访问控制,例如 @private、@protected 等。
我曾考虑过以某种方式生成单独的结构,如下所示:
@interface Foo {
int bar;
@private
int baz;
@public
int qux;
}
=>
类似于
struct Class_Foo_Protected {
int bar;
};
struct Class_Foo_Private {
int baz;
};
struct Class_Foo_Public {
int qux;
};
但我真的不知道。有人知道这是怎么做到的吗?
In Objective-C, I'm curious how access controls for instance variables, like @private
,@protected
, etc. are implemented.
I had considered that separate structures were being generated in some way like this:
@interface Foo {
int bar;
@private
int baz;
@public
int qux;
}
=>
something along the lines of
struct Class_Foo_Protected {
int bar;
};
struct Class_Foo_Private {
int baz;
};
struct Class_Foo_Public {
int qux;
};
But I really have no idea. Anyone know how this was really done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些修饰符不会改变类的内存布局。编译器本身会记住哪个 ivar 是公共的、受保护的或私有的,并且如果您尝试从不适当的地方访问它们,则会发出错误。这一切都是在生成任何代码之前完成的,不会影响生成的代码。
Those modifiers don’t change anything about the memory layout of your class. The compiler itself remembers which ivar is public, protected or private and emits errors if you try to access them from somewhere inappropriate. This is all done before any code is generated and doesn’t affect the generated code.