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 ...
随机推荐
- Kafka项目实战-用户日志上报实时统计之分析与设计
1.概述 本课程的视频教程地址:<Kafka实战项目之分析与设计> 本课程我通过一个用户实时上报日志案例作为基础,带着大家去分析Kafka这样一个项目的各个环节,从而对项目的整体设计做比 ...
- linux装tomcat遇到的坑
最开始通过apt-get安装,各种毛病 然后下载tar.gz压缩包解压使用,运行startup.sh可以启动,但是看日志发现 Tomcat启动时卡在 INFO HostConfig.deployDir ...
- mysql创建表时,设置timestamp DEFAULT NULL报错1067 - Invalid default value for 'updated_at'
问题背景: 线上的linux服务器上的mysql服务器中导出数据库的结构.想要在本地创建一个测试版本 导出后再本地mysql上运行却报错 1067 - Invalid default value ...
- 三、TortoiseGit之配置密钥
TortoiseGit使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥. 也就是说使用 ssh-keygen -t rsa -C "username@email.co ...
- 自己写的thinkphp自动生成类
模型类:CqhModel.class.php <?php namespace Cqh\Model; use Think\Model; class CqhModel extends Model { ...
- FLV 封装格式解析
本文为作者原创,转载请注明出处:https://www.cnblogs.com/leisure_chn/p/10662941.html FLV (Flash Video) 是由 Adobe 公司推出的 ...
- 在整合spring和mongodb中,提示at org.springframework.data.mapping.model.BasicPersistentEntity.findAnnotation(
遇到这种坑,找了好多资料.基本是都是因为springdata的jar包和spring的版本不兼容导致,除了这个错误之外,还有会比较多其他错误,也是版本不兼容导致的. at org.springfram ...
- minitab的鱼骨图的制作
Minitab的操作路径为:主菜单Stat > Quality Tools > Cause-and-Effect 先分别选择列"Man.Machine.Material.Meth ...
- Spring基于注解和XML混合方式的使用
首先要明白,基于注解和XML两种方式的实现功能是一样的,只是两种不同的配置方式. 一.IoC配置 1.配置xml 在使用注解与xml结合的方式配置IoC之前,首先要引入context标签: xmlns ...
- WordPress 新版本中编辑器不好用, 使用原有编辑器
将以下代码添加到当前主题的函数模板 functions.php 文件中即可. add_filter('use_block_editor_for_post', '__return_false'); re ...