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平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.本文继续上一篇随笔<从 ...
随机推荐
- Django实战(5):引入bootstrap,设置静态资源
之前生成了Product类的scaffold,但是如同rails的开发者David所讲的那样,scaffold几乎没什么用.所以按照<Agile Web Development with Rai ...
- gitlab-针对API,获取私有令牌
Gitlab有一个强大的API系统,几乎所有的功能都可以在web中执行,当然也可以通过API来执行,为了使用API,需要从Gitlab中获取私有token. 执行步骤: 1. 登陆Gitlab服务器 ...
- 终端(terminal)、tty、shell、控制台(console)、bash之间的区别与联系
1.终端(terminal) 终端(termimal)= tty(Teletypewriter, 电传打印机),作用是提供一个命令的输入输出环境,在linux下使用组合键ctrl+alt+T打开的就是 ...
- linux查询系统负载
linux uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令过去只显示系统运行多久.现在,可以显示系统已经运行了多长时间,信息显示依次为:现在时间.系统已经运行 ...
- ubuntu下安装低级版本gcc/g++ 并随意切换
来自:http://blog.sina.com.cn/s/blog_6cee149d010129bl.html 发现Android的版本中编译Host的程序经常因为本机的Gcc版本过高,需要这样那样的 ...
- linux 下nginx安装
一.一键安装四个依赖 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 二.创建一个安装目录,并下载nginx安装 ...
- softmax 杂谈
在多分类问题中,我们可以使用 softmax 函数,对输出的值归一化为概率值.下面举个例子: import sys sys.path.append("E:/zlab/") from ...
- shell script执行的几种方式
编写一个shell脚本test.sh,内容如下 a='测试执行方式' echo $a 方式1 使用路径的方式执行 chmod a+x test.sh ./test.sh 执行结果如下 当脚本执行之后, ...
- Sting.format字符串格式化
控制格式scanf printf 也不知道为什么=-= 越研究深层的java就越感觉它是从别的语言那抄袭来的
- [leetcode sort]57. Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...