--------------- FKCar.h ---------------
#import
<Foundation/Foundation.h>
@interface
FKCar :
NSObject
@property ( nonatomic , copy ) NSString * brand;
@property ( nonatomic , copy ) NSString * model;
- ( void ) drive;
@property ( nonatomic , copy ) NSString * brand;
@property ( nonatomic , copy ) NSString * model;
- ( void ) drive;
@end
--------------- FKCar.m ---------------
#import
"FKCar+drive.h"
@implementation
FKCar
- ( void ) drive;
{
- ( void ) drive;
{
NSLog(@"%@汽车正在路上奔驰", self);
}
- ( void ) drive:( NSString *) owner
{
- ( void ) drive:( NSString *) owner
{
NSLog(@"%@正驾驶%@汽车在路上奔驰", owner , self);
}
- ( NSString *) description
{
- ( NSString *) description
{
return [NSString stringWithFormat:@"<FK[_brand=%@,_model=%@,_color=%@]>"
,
self
.
brand
,
self
.
model
,
self
.
color
];
}
@end
--------------- FKCar+drive.h ---------------
#import
"FKCar.h"
@interface FKCar ()
@property ( nonatomic , copy ) NSString * color;
- ( void ) drive:( NSString *)owner;
@interface FKCar ()
@property ( nonatomic , copy ) NSString * color;
- ( void ) drive:( NSString *)owner;
@end
---------------
main.m
---------------
#import
<Foundation/Foundation.h>
#import
"FKCar+drive.h"
int main()
{
FKCar * car = [[ FKCar alloc ] init ];
car. brand = @" 宝马 " ;
car. model = @"X5" ;
car. color = @" 黑色 " ;
[car drive ];
[car drive : @" 孙悟空 " ];
int main()
{
FKCar * car = [[ FKCar alloc ] init ];
car. brand = @" 宝马 " ;
car. model = @"X5" ;
car. color = @" 黑色 " ;
[car drive ];
[car drive : @" 孙悟空 " ];
}
一、编写本节代码的具体步骤:
1.可仿照第二章001节的代码编写步骤。
2.FKCar+drive.h文件需要另行创建。
二、本节代码涉及到的知识点:
1.拓展的写法是 @interface 加上 已有类的类名 加上 () 加上 花括号。
花括号里面写实例变量和方法。
2.注意:拓展可以增加实例变量和方法,而前面学的category类别只能增加方法。
3.注意,FKCar.m文件中,要导入FKCar+drive.h文件。