11-Category

1. Classification.
category, category, category
2. Write a student class: there are many methods in the class.
Eating, drinking, sleeping....Basic behavior
Learn, code, write books.... learn
Play Dota Play LOL Play CF.... Play
Climbing, running, playing football... Sports
......
If these methods are written in the same class module. Of course it is completely possible.
If it is all written in one module, it will be very bloated. It will be difficult to maintain and manage later.
By default, 1 class occupies 1 module. This is to write all members in this 1 module. It is very male management.
Our idea: Let one class occupy multiple modules. Define methods with similar functions in the same module.
Such benefits: easy maintenance and management.
How to divide a class into multiple modules?
3. Classification:
1). As the name implies: divide a class into multiple modules.
2). How to add classification for 1 class.
3). It will generate 1 .h and 1 .m module.
a. The file name of the module: this class name + category name.h this class name + category name.m
UIView+Util.h UIView+Util.m
4). The added category is also divided into declaration and implementation.
@interface This class name (category name)
@end
It means not to create a new class. Instead, add a category to an existing class. Write the name of the category in parentheses.
Because a class can add multiple categories in order to distinguish each category. So the category should be named.
@implementation Student (itcast)
@end
This is the implementation of the classification.
4. Use of Classification.
1) If you want to access the members defined in the category, you need to import the header file of the category.
5. The role of classification: Divide a class into multiple modules.
--------------------------------------------
A few places to pay attention to when using classification:
1. Classification can only add methods, not attributes
2. 在分类之中可以写@property 但是不会自动生成私有属性. 也不会自动生成getter setter的实现.
只会生成getter setter的声明.
所以,你就需要自己写getter 和 setter的声明. 也需要自己定义属性 这个属性就必须在本类中.
3. 在分类的方法实现中不可以直接访问本类的真私有属性(定义在本类的@implementation之中)
但是可以调用本类的getter setter来访问属性.
本类的@property生成的私有属性,只可以在本类的实现中访问.
分类中不能直接访问私有属性 真.
分类可以使用 getter setter 来访问.
4. 分类中可以存在和本类同名方法的.
当分类中有和本类中同名的方法的时候,优先调用分类的方法.哪怕没有引入分类的头文件.
如果多个分类中有相同的方法,优先调用最后编译的分类.
------------------------------------
什么时候需要使用分类.
当1个类的方法很多很杂的时候. 当1个类很臃肿的时候.
那么这个时候我们就可以使用分类. 将这个类分为多个模块.将功能相似的方法写在同1个模块之中.



Guess you like

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