Objective-C:分类(Category、extension)
//扩展中定义的都是类中私有的实例变量和方法
(二)分类的使用
(1)分类只能增加方法(包括类方法和对象方法),不能增加成员变量
(2)在分类方法的实现中可以访问原来类中的成员变量;
(3)分类中可以重新实现原来类中的方法,但是会覆盖掉原来的方法,导致原来的方法无法再使用(警告);
(4)方法调用的优先级:分类->原来的类->父类,若包含有多个分类,则最后参与编译的分类优先;
(5)在很多的情况下,往往是给系统自带的类添加分类,如NSObject和NSString,因为有的时候,系统类可能并不能满足我们的要求。
(6)在大规模的应用中,通常把相应的功能写成一个分类,可以有无限个分类,对原有类进行扩充,一般分模块写,一个模块一个分类。
//自定义的字符串类String
// String.h
// 类别
//
// Created by ma c on 15/8/12.
// Copyright (c) 2015年. All rights reserved.
// #import <Foundation/Foundation.h> @interface String : NSObject
@property(nonatomic,strong)NSString* string;
-(id)initWithString:(NSString*) str;
-(void)print;
@end
// String.m
// 类别
//
// Created by ma c on 15/8/12.
// Copyright (c) 2015年. All rights reserved.
// #import "String.h"
//#import "String_String_private.h"//在String类单独导入一个类扩展文件 //类扩展extension,添加的属性str1和show方法都是私有的,只能在String类中可以访问得到
@interface String()//直接将类扩展文件写入String类的实现中
{
NSString *str1;
}
-(void)show;
@end @implementation String
-(id)initWithString:(NSString*) str
{
self = [super init];
if(self)
{
_string = str;
}
return self;
}
-(void)show
{
NSLog(@"string is runing");
}
-(void)print//通过print方法访问到类扩展中添加的属性和方法
{
[self show];
NSLog(@"%@",_string);
}
@end
//单独的类扩展文件
// String_String_private.h
// 类别
//
// Created by ma c on 15/8/12.
// Copyright (c) 2015年. All rights reserved.
// #import "String.h" @interface String ()
{
NSString *str1;
}
//@property(nonatomic,strong)NSString* str1;
-(void)show;
@end
//类别文件
// String+Count.h
// 类别
//
// Created by ma c on 15/8/12.
// Copyright (c) 2015年. All rights reserved.
// #import "String.h"
//String的类别Count,添加的方法,对String类进行功能的扩展(切记:类别中只能添加方法)
@interface String (Count)
-(NSInteger) nmuberOfString: (NSString *)str;
-(NSInteger) numberOfCount: (NSString *)str;
@end
// String+Count.m
// 类别
//
// Created by ma c on 15/8/12.
// Copyright (c) 2015年. All rights reserved.
// #import "String+Count.h" @implementation String (Count)
-(NSInteger) nmuberOfString: (NSString *)str
{
NSInteger count = ;
for(int i=;i<str.length;i++)
{
unichar ch = [str characterAtIndex:i];
if(ch>='' && ch<='')
count++;
}
return count;
}
-(NSInteger) numberOfCount: (NSString *)str
{
NSInteger count = ;
for(int i=;i<str.length;i++)
{
unichar ch = [str characterAtIndex:i];
if(ch>='' && ch<='')
count++;
}
return count;
}
@end
//主函数测试
// main.m
// 类别
//
// Created by ma c on 15/8/12.
// Copyright (c) 2015年. All rights reserved.
// #import <Foundation/Foundation.h>
#import "String+Count.h"
#import "String.h"
int main(int argc, const char * argv[])
{
@autoreleasepool
{
//测试类扩展(未命名的类别) 关键字:extension
String *string = [[String alloc]initWithString:@"xiayuan"];
[string print]; //测试类别(命名的类别) 关键字:Category
NSInteger a = [string nmuberOfString:@"xu3984jjsn6ew32"];
NSLog(@"a = %ld",a); NSInteger b = [string numberOfCount:@"36y3jfse93u4gdf8"];
NSLog(@"b = %ld",b);
}
return ;
}
//运行结果
2015-08-12 15:24:06.281 类别[1304:75695] string is runing
2015-08-12 15:24:06.282 类别[1304:75695] xiayuan
2015-08-12 15:24:06.282 类别[1304:75695] a = 7
2015-08-12 15:24:06.282 类别[1304:75695] b = 7
Program ended with exit code: 0
Objective-C:分类(Category、extension)的更多相关文章
- Objective-C分类 (category)和扩展(Extension)
1.分类(category) 使用Object-C中的分类,是一种编译时的手段,允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以 ...
- OC中分类(Category)和扩展(Extension)
1.分类的定义 category是Objective-C 2.0之后添加的语言特性,中文也有人称之为分类.类别.Category的主要作用是为已经存在的类添加方法.这个大家可能用过很多,如自己给UIC ...
- Objective-C:继承、分类(Category、extension)、协议(protocol),个人理解,仅供参考
总结:继承.分类(Category.extension).协议(protocol) 一.继承: (1)特点: 继承多用于一般父类中的方法功能比较齐全,子类从父类继承过来使用,可以省略很多重复的代码 ...
- 分类(Category)的本质 及其与类扩展(Extension) /继承(Inherit)的区别
1.分类的概念 分类是为了扩展系统类的方法而产生的一种方式,其作用就是在不修改原有类的基础上,为一个类扩展方法,最主要的是可以给系统类扩展我们自己定义的方法. 如何创建一个分类?↓↓ ()Cmd+N, ...
- iOS中 分类(category)与扩展(Extension)的区别?
1.分类(category)的作用 (1).作用:可以在不修改原来类的基础上,为一个类扩展方法.(2).最主要的用法:给系统自带的类扩展方法. 2.分类中能写点啥? (1).分类中只能添加“方法”,不 ...
- App开发流程之使用分类(Category)和忽略编译警告(Warning)
Category使得开发过程中,减少了继承的使用,避免子类层级的膨胀.合理使用,可以在不侵入原类代码的基础上,写出漂亮的扩展内容.我更习惯称之为"分类". Category和Ext ...
- Runtime - ③ - 分类Category探究
写博客只是为了让自己学的更深刻,参考:https://tech.meituan.com/DiveIntoCategory.html 分类(Category)是个啥玩意儿这里就不多介绍了,这里主要是研究 ...
- iOS之分类(category)
1.分类(category)的作用 1.1作用:可以在不修改原来类的基础上,为一个类扩展方法.1.2最主要的用法:给系统自带的类扩展方法. 2.分类中能写点啥? 2.1分类中只能添加“方法”,不能增加 ...
- 从C#到Objective-C,循序渐进学习苹果开发(3)--分类(category)和协议Protocal的理解
本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本文继续上一篇随笔<从 ...
随机推荐
- hdoj2544 最短路(Dijkstra || Floyd || SPFA)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路 最短路算法模板题,求解使用的Dijkstra算法.Floyd算法.SPFA算法可以当做求解 ...
- Cygwin镜像使用
前言:Cygwin是一个在windows平台上运行的类UNIX模拟环境,可以自己安装想用的插件 Cygwin镜像使用帮助 收录架构 x86 x86_64 收录版本 所有版本 更新时间 每12小时更新一 ...
- MySQL用户授权 和 bin-log日志 详解和实战
看了上一篇博文的发布时间,到目前已经有三个月没更新博文了.这三个月经历了很多事情,包括工作.生活和感情等等.由于个人发展的原因,这个月准备换工作啦.在这段时间,我会把Web大型项目中所接触到的技术都总 ...
- Mysql自增语句
一.创建查询 二.将 alter table `表名` change id id int not null auto_increment UNIQUE;复制进去(以id为例) 三.运行ok 注意:手动 ...
- codevs 5790 素数序数
5790 素数序数(筛素数版) 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个整数n,保证n为正整数且在int范 ...
- servlet生命周期和执行流程
一 .生命周期 servlet 声明周期可以分四个阶段: 类装载过程 init() 初始化过程 service() 服务过程,选择doGet \ doPost destroy() 销毁过程 servl ...
- Nginx负载均衡的五种策略
nginx可以根据客户端IP进行负载均衡,在upstream里设置ip_hash,就可以针对同一个C类地址段中的客户端选择同一个后端服务器,除非那个后端服务器宕了才会换一个. nginx的upstre ...
- js文件命名冲突理解
在一个index.html文件里先后导入a.js和b.js文件a.js文件里写上var s = 2;console.log(s);b.js文件里写上var s = 5;这时a.js和b.js用了相同的 ...
- hadoop 2.7.1 高可用安装部署
hadoop集群规划 目标:创建2个NameNode,做高可用,一个NameNode挂掉,另一个能够启动:一个运行Yarn,3台DataNode,3台Zookeeper集群,做高可用. 在hadoop ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...