Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Objective-C 中公开 iVar 是否违背了封装的目的?
@interface SampleClass{ @public int totalRecords; }
简而言之,是的。
对 ivars 的直接访问意味着在读取或写入SampmleClass时,您的子类或未来版本无法实现任何类型的自定义行为。totalRecords
SampmleClass
totalRecords
这就是为什么访问对象状态是通过方法无处不在,但在类本身的实现中(即使那样,它也主要是通过方法)。