2022-10-01objective-c 分类

1>分类的作用:将一个类分为多个模块
2>分类的使用,如果要访问分类中的成员,需要将分类的头文件先引入进来。
例子:
新建一个student的类

//头文件
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Student : NSObject
-(void)haha;
@end

NS_ASSUME_NONNULL_END
//.M文件
#import "Student.h"

@implementation Student
-(void)haha{
    NSLog(@"hahahahhahah");
}
@end

新建一个Student的分类

#import "Student.h"

NS_ASSUME_NONNULL_BEGIN

@interface Student (itcast)
-(void)hehe;
@end

NS_ASSUME_NONNULL_END
//.M文件
#import "Student+itcast.h"

@implementation Student (itcast)
-(void)hehe{
    NSLog(@"hehehehehehe呵呵呵");
}
@end

调用

#import <Foundation/Foundation.h>
#import "Student.h"
#import "Student+itcast.h"
#import "Student+Sb.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        Student *stu=[Student new];
        [stu haha];
        [stu hehe];
        [stu tsb];
    }
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容