Objective-C 类的复合
2016-03-13 00:15
603 查看
// // main.m // Study // // Created by DwightDing on 16/3/12. // Copyright © 2016年 DwightDing. All rights reserved. // #import <Foundation/Foundation.h> @interface Tire : NSObject @end//Tire @implementation Tire -(NSString *)description{ return (@"I'm a Tire, I last a while"); }//description在Cocoa中定义 @end @interface Engine : NSObject @end @implementation Engine -(NSString *)description { return (@"I am an engine.Vroom"); } @end @interface Car : NSObject { Engine *engine; Tire *tires[4]; } -(void)print; @end @implementation Car -(id) init { if(self = [super init]){ engine = [Engine new]; tires[0] = [Tire new]; tires[1] = [Tire new]; tires[2] = [Tire new]; tires[3] = [Tire new]; } return self; } -(void)print { NSLog(@"%@",engine); NSLog(@"%@",tires[0]); NSLog(@"%@",tires[1]); NSLog(@"%@",tires[2]); NSLog(@"%@",tires[3]); } @end int main(int argc, const char * argv[]) { Car *car; car = [Car new]; [car print]; return 0; }
对类进行扩展:
//
// main.m
// Study
//
// Created by DwightDing on 16/3/12.
// Copyright © 2016年 DwightDing. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Tire : NSObject
@end//Tire
@implementation Tire
-(NSString *)descrip
4000
tion{
return (@"I'm a Tire, I last a while");
}//description在Cocoa中定义
@end
//Define Engine
@interface Engine : NSObject
@end
//setter getter
@implementation Engine
-(NSString *)description
{
return (@"I am an engine.Vroom");
}
@end
//define Car
@interface Car : NSObject
{
Engine *engine;
Tire *tires[4];
}
-(void)setTire : (Tire *)tire atIndex : (int) index;
-(Tire *)tireAtIndex : (int) index;
-(Engine *) engine;
-(void) setEngine : (Engine *) newEngine;
-(void)print;
@end
@implementation Car
//realize function
-(void)setTire : (Tire *)tire atIndex : (int) index
{
if (index<0||index>3) {
NSLog(@"error");
exit(1);
}
tires[index]=tire;
}
-(Tire *)tireAtIndex : (int) index
{
if (index<0||index>3) {
NSLog(@"error");
exit(1);
}
return (tires[index]);
}
-(void) setEngine : (Engine *) newEngine
{
engine=newEngine;
}
-(Engine *) engine
{
return (engine);
}
-(void)print
{
NSLog(@"%@",engine);
NSLog(@"%@",tires[0]);
NSLog(@"%@",tires[1]);
NSLog(@"%@",tires[2]);
NSLog(@"%@",tires[3]);
}
@end
int main(int argc, const char * argv[]) {
Car *car = [Car new];
Engine *engine=[Engine new];
[car setEngine:engine];
for (int i = 0; i < 4; i++) {
Tire *tire = [Tire new];
[car setTire:tire atIndex:i];
}
[car print];
return 0;
}
相关文章推荐
- python的class(类)中的object是什么意思?
- Json反序列化之ObjectMapper(自定义实现反序列化方法)
- 第一章-熟悉Objective-C
- Object-c对象和消息传递
- Objective-c 深浅复制
- 从C/C++到Objective-C(五)--- 类别的使用
- android.os.DeadObjectException memory near r0: 异常处理
- js中object、字符串与正则表达式的方法
- Objective-c 集合对象
- 分离出表单对象(Form Objects)
- MyBatis中对List<Object> 对象List的批处理插入操作
- 从C/C++到Objective-C(四)--- 属性的使用
- Object-C HelloWorld 编译运行
- Objective-c 字典对象
- Android之–Activity通过Intent传递Object
- Object-C与Swift混合开发
- git :设置 object-c 的忽略文件
- Example rebuttal for "YOLO: Real-Time Object Detection"
- objective-c HelloWorld实践 (gcc编译器)
- Java Object Layout(jol)