12-Extension Extension

1. Extension: Extension
1). Is a special category. So extension is also part of the class.
2). Special features:
a. Extending this particular category has no name.
b. Only the declaration has no implementation. It shares an implementation with this class.
2. Extended syntax
grammar;
@interface classname()

@end
No implementation. Shares 1 implementation with this class.
3. Steps to add extensions to the class
There is only 1 .h file. File name: class name_get file name.h
There are only extended declarations in this file.
@interface Person ()
@end
4. Basic use of extension.
1). The essence of extension is a classification. As part of this class.
Just a special category
no name.
2). Extensions only have declarations, no separate implementations. Share an implementation with this class.
5. The difference between extension and classification
1). Categories have names. Extending without names is an anonymous category.
2). Each category has a separate declaration and implementation. The extension only declares that there is no separate implementation and this class shares one implementation,
3). Only new methods can be added to the category. Any member of the extension can be written.
4). @property can be written in the category but only the declaration of the getter setter will be generated.
Writing @property in the extension will automatically generate private properties and also generate getter setter declarations and implementations.
6. Extended application scenarios.
1). To write a private @property for the class.
The generated getter and setter methods can only be accessed inside the class and cannot be accessed outside.
In fact, we can think: @property generates private properties, generates getter setter implementations, and does not declare them.
2). In the case of 100% extension, it will not monopolize 1 file. The extension is directly written in the implementation file of this class.
At this time, the members written in the extension are equivalent to the private members of this class. They can only be accessed in the implementation of this class.
Not accessible from outside.
3). 什么时候使用延展?
当我们想要为类定义私有成员的时候,就可以使用延展. 将延展定义在这个类的实现文件中.
如果想要为类写1个真私有属性,虽然我们可以定义在@implementation之中.但是不要这么写 这样很不规范.
写1个延展.将这个私有属性定义在延展中.
如果要为类写1个私有方法,建议将声明写在延展中, 实现写在本类的实现中. 提供代码的阅读性
如果想要为类写1个私有的@property 就直接写在延展就可以了.
4). 延展天生就是来私有化类的成员的.
如果类的成员只希望在类的内部访问,那么就将其定义在延展中.
如果类的成员允许被外界访问 定义在本类的@interface中.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901676&siteId=291194637