zoukankan      html  css  js  c++  java
  • OC之category的用法

    • 简介
    • 实例
    • 好处

    一、category简介

    动态的为某个已经存在的类增加方法,不可以增加成员变量

    二、实例

    //
    //  Student+CatetoryStudent.h
    //  Category
    //
    //  Created by apple on 14-3-26.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import "Student.h"
    
    @interface Student (CatetoryStudent)
    
    -(void) test;
    
    @end
    //
    //  Student+CatetoryStudent.m
    //  Category
    //
    //  Created by apple on 14-3-26.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import "Student+CatetoryStudent.h"
    
    @implementation Student (CatetoryStudent)
    
    -(void)test
    {
        NSLog(@"test");
    }
    
    @end
    //
    //  main.m
    //  Category
    //
    //  Created by apple on 14-3-26.
    //  Copyright (c) 2014年 apple. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Student.h"
    #import "Student+CatetoryStudent.h"
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            Student *stu = [[[Student alloc] init] autorelease];
            
            [stu test];
            
        }
        return 0;
    }

    三、总结

    在什么时候用category比较好?

    1⃣️需求改变

    2⃣️土堆合作

    3⃣️对系统类扩展,比如给NSString类增加一个处理Json的方法

  • 相关阅读:
    [python第七课]字符串和常用数据结构
    深浅拷贝与循环引用问题
    CSS居中总结
    CSS布局总结
    跨域
    函数节流与防抖
    浏览器渲染原理及渲染阻塞
    进程与线程
    前端之网络攻击
    前端之缓存
  • 原文地址:https://www.cnblogs.com/letougaozao/p/3625871.html
Copyright © 2011-2022 走看看