Categories VS Extensions (分类 vs 扩展)

#import <Foundation/Foundation.h> @interface Car : NSObject @property(copy,nonatomic)NSString *model;
@property(readonly) double odometer; -(void)startEngine;
-(void)drive;
-(void)turnLeft;
-(void)turnRight;
@end
@implementation Car -(void)startEngine
{
NSLog(@"starting the %@'s engine" ,_model);
}
-(void)drive
{
NSLog(@"The %@ is now driving",_model);
}
-(void)turnLeft
{
NSLog(@"The %@ is turning left",_model);
}
-(void)turnRight
{
NSLog(@"The %@ is turn right",_model);
}
@end

#import "Car+Maintenance.h" @implementation Car (Maintenance) @end
#import "Car.h" @interface Car (Maintenance) @end
#import "Car.h" @interface Car (Maintenance) -(BOOL)needsOilChange;
-(void)changeOil;
-(void)rotateTires;
-(void)jumpBatteryUsingCar:(Car *)anotherCar;
@end
#import "Car+Maintenance.h"
@implementation Car (Maintenance)
-(BOOL)needsOilChange {
    return YES;
}
-(void)changeOil {
    NSLog(@"Changin oil for the %@",[self model]);
}
-(void)rotateTires {
    NSLog(@"Rotation tires for the %@",[self model]);
}
-(void)jumpBatteryUsingCar:(Car *)anotherCar {
    NSLog(@"Jumped the %@ with a %@",[self model],[anotherCar model]);
}
@end
#import "Car+Maintenance.h"
#import "Car.h"
#import "Car+Protected.h" @implementation Car
...
- (void)drive {
[self prepareToDrive];
NSLog(@"The %@ is now driving", _model);
}
...
就像上边这样。
#import "Car.h"
@interface Car ()
-(BOOL)engineIsWorking;
@end
#import "Car.h"
#import "Car_Car.h"
@implementation Car -(void)startEngine
{
NSLog(@"starting the %@'s engine" ,_model);
}
-(void)drive
{
NSLog(@"The %@ is now driving",_model);
}
-(void)turnLeft
{
NSLog(@"The %@ is turning left",_model);
}
-(void)turnRight
{
NSLog(@"The %@ is turn right",_model);
}
-(BOOL)engineIsWorking {
return YES;
}
@end
#import "Car.h" @interface Car ()
@property(readwrite) double odometer;
-(BOOL)engineIsWorking;
@end
Categories VS Extensions (分类 vs 扩展)的更多相关文章
- iOS开发---分类和扩展(Categories和Extensions)
		
1.分类能够做到的事情主要是:即使在你不知道一个类的源码情况下,向这个类添加扩展的方法. 此外,分类能够保证你的实现类和其他的文件区分开. 1 #import “UIViewControl ...
 - (转)iOS分类和扩展(Categories和Extensions)
		
分类(Category) 分类能够做到的事情主要是:即使在你不知道一个类的源码情况下,向这个类添加扩展的方法. 此外,分类能够保证你的实现类和其他的文件区分开. #import “UIView ...
 - iOS 类别和扩展(Categories和Extensions)
		
分类(Category) 分类能够做到的事情主要是:即使在你不知道一个类的源码情况下,向这个类添加扩展的方法. 此外,分类能够保证你的实现类和其他的文件区分开. 1 #import “UI ...
 - iphone 开发Categories 、Extensions 区别 --转
		
Category和Extension这两个概念,即便对于Java程序员,可能也会感到陌生.这是Objective C为程序员提供的两个强大的动态机制——简单地说,它们允许程序员为已有的对象添加新的方法 ...
 - 8.2.1.7 Use of Index Extensions 使用索引扩展
		
8.2.1.7 Use of Index Extensions 使用索引扩展 InnoDB 自动扩展每个secondary index 通过添加primary key columns to it,考虑 ...
 - iOS项目开发日常之创建文件(协议、类、分类、扩展)
		
iOS项目开发过程中,是以不断创建文件的形式进行着的. 创建得比较频繁的文件类型是: 这两个类型中创建的文件有:子类.分类.扩展.协议四种文件,如下: 这四类文件是频繁创建的,我们来看一下各自分 ...
 - iOS中的分类和扩展
		
一.什么是分类? 概念:分类(Category)是OC中的特有语法,它是表示一个指向分类的结构体指针.根据下面源码组成可以看到它没有属性列表,原则上是不能添加成员变量(其实可以借助运行时功能,进行关联 ...
 - Swift基础--Swift中的分类以及在分类中扩展init方法的注意事项
		
Swift中的分类 1.创建一个空的swift文件 2.关键字extension,格式: extension 要扩展的类名 {} extension UIButton { } Swift中扩展init ...
 - Celery-4.1 用户指南: Extensions and Bootsteps (扩展和Bootsteps)
		
自定义消息消费者 你可能想要嵌入自定义的 Kombu 消费者来手动处理你的消息. 为了达到这个目的,celery 提供了一个 ConsumerStep bootstep 类,你只需要定义 get_co ...
 
随机推荐
- openshift上使用devicemapper
			
环境:openshift v3.6.173.0.5 openshift上devicemapper与官方文档中的描述略有不同,在官方文档的描述中,容器使用的lvm文件系统挂载在/var/lib/devi ...
 - 多线程读者写者问题--用QT实现
			
先把代码贴上来,有时间再整理吧..因为工作中用Qt,所以用Qt实现的....刚上班,,忙! 三种方法,读者优先,写者优先和公平竞争.. 读者优先 #include <QCoreApplicati ...
 - spring AOP 之二:@AspectJ注解的3种配置
			
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
 - Delphi常用快捷键
			
delphi是我学编程时的入门语言,用过一年多的时光,个人对它还是挺喜欢的.现在用的少了,一些快捷键和语法也有些遗忘了,这里对delphi的快捷键做个总结,留个纪念.嘿嘿,不知道还有多少人还用着这门语 ...
 - mysql计算两个日期相差的天数
			
DATEDIFF() 函数可以返回两个日期之间的天数. 如下: SELECT DATEDIFF('2015-06-29','2015-06-12') AS DiffDate 结果得17 SELECT ...
 - Mybatis 3 配置 Log4j
			
Mybatis与Log4j 最常用的日志输出是Log4j,将相应的jar包和配置文件放到相应的位置,Mybatis就可以通过Log4j将SQL语句打印出来. 配置Log4j.properties 将l ...
 - html特殊字体显示
			
1.下载需要显示的ttf字体. 2.css样式调用. /* 微软雅黑 */ @font-face { font-family: microsoftyahei; src: url('${pageCont ...
 - 【Json】1、JSON 数据格式
			
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于人阅读和编写,同时也易 ...
 - js之模态对话框
			
目标效果:点击页面按钮,显示模态对话框,在模态对话框里点击取消关闭模式对话框. 效果如下 实现代码如下: <!DOCTYPE html> <html lang="en&qu ...
 - JS的DOM操作 - 你真的了解吗?
			
摘要 想稍微系统的说说对于DOM的操作,把Javascript和jQuery常用操作DOM的内容归纳成思维导图方便阅读,同时加入性能上的一些问题. 前言 在前端开发的过程中,javascript极为重 ...