zoukankan      html  css  js  c++  java
  • OC语法9——Category类别

    Category(分类):

      当我们在开发过程中要给类添加新的方法时,一般不要去动原类。

      再不改动原类的限制下,怎么拓展类的方法?以往我们的做法是新建子类使其继承该类,然后通过子类拓展类的行为。

      OC提供了一种全新的方法:Category(分类)。在不改动原类的基础上动态的拓展类的行为。

      假如我们要动态拓展Student类,则应该建一个分类(Category),注意:文件命名要有规范,Student+StuOthers.h

      格式:只要在被扩展的类名(原类)后加(),在括号里写分类名(拓展类名);

        Student+StuOthers.h中

    #import "Student.h"
    @interface Student (StuOthers)   //括号里就是分类名
    - void test();
    @end

        Student+StuOthers.m中

    #import "Student+StuOthers.h"
    @implementation Student (StuOthers)
    - void test()
    {
        @NSLog(@"wanger");
    }
    @end
  • 相关阅读:
    flex-grow带来的排版问题
    css文本样式,空格
    第八周作业
    JSON简介
    Ajax
    java applet小程序
    java ee 部分分析
    xml相关知识
    JAVA EE体系结构图
    java EE初次理解
  • 原文地址:https://www.cnblogs.com/wangerxiansheng/p/4297296.html
Copyright © 2011-2022 走看看