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 ...
随机推荐
- Java并发编程笔记之CyclicBarrier源码分析
JUC 中 回环屏障 CyclicBarrier 的使用与分析,它也可以实现像 CountDownLatch 一样让一组线程全部到达一个状态后再全部同时执行,但是 CyclicBarrier 可以被复 ...
- Solidity中uint转string
在<Solidity中uint转bytes>中,我们知道unit如何转换成bytes,其实把uint转换成string,就是在最后加上string(bytes变量)即可,如下所示: pra ...
- ECMAScript typeof用法
typeof 返回变量的类型字符串值 .其中包括 “object”.“number”.“string”.“undefined”.“boolean”. 1.在变量只声明.却不初始化值 Or 在变量没 ...
- C# 字符串拼接性能探索
本文通过ANTS Memory Profiler工具探索c#中+.string.Concat.string.Format.StringBuilder.Append四种方式进行字符串拼接时的性能. 本文 ...
- [PHP] 算法-找出两个链表的第一个公共结点的PHP实现
输入两个链表,找出它们的第一个公共结点 1.两个单链表,有公共结点,那么必然,尾部公用 2.找出链表1的长度,找出链表2的长度,长的链表减去短的链表得出一个n值 3.长的链表先走n步,两个链表再同时移 ...
- Java中变量之局部变量、本类成员变量、父类成员变量的访问方法
变量:局部变量.本类成员变量.父类成员变量 如何访问:如果变量名相同,则采用就近原则,哪个变量离所要调用的访问最近,那就么就输出,优先顺序为:局部变量 > 本类成员变量 > 父类成员变量 ...
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(第三部分)
Earthstone Keeper Time Limit: 4 Seconds Memory Limit: 65536 KB Earthstone Keeper is a famous ro ...
- spline和Pchips的不同(matlab)
这是一条官方的链接:http://blogs.mathworks.com/cleve/2012/07/16/splines-and-pchips/ 主要是比较在matlab中spline和Pchips ...
- java - 并发编程易错实例
生产者消费者问题 https://juejin.im/post/5aeec675f265da0b7c072c56 notify()发生在wait()之前会怎么样?怎么处理? wati()等待条件的变化 ...
- Python:dictionary
# Python3.4 Eclipse+PyDev 打开Eclipse,找到Help菜单栏,进入Install New Software…选项. # 点击work with:输入框的旁边点击Add…, ...