生活随笔
收集整理的這篇文章主要介紹了
Object-C学习代码【简单的Car程序】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
//
// CarObject.h
// Car
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.car. All rights reserved.
//#import <Foundation/Foundation.h>@interface Tire : NSObject
@end // Tire@interface Engine : NSObject
@end // Engine@interface Car : NSObject
{Engine *engine;Tire *tires[4];
}
- (void) print;
@end // Car //
// CarObject.m
// Car
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.car. All rights reserved.
//#import "CarObject.h"@implementation Tire
- (NSString *) description {return (@"I am a tire. I last a while.");
} // description
@end // Tire@implementation Engine
- (NSString *) description {return (@"I am an engine. Vrooom!");
} // description
@end // Engine@implementation Car
// 返回的是id類型,id即為泛型對象指針
- (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);
} // init
- (void) print {NSLog(@"%@", engine);NSLog(@"%@", tires[0]);NSLog(@"%@", tires[1]);NSLog(@"%@", tires[2]);NSLog(@"%@", tires[3]);
} // print
@end // Car //
// main.m
// Car
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.car. All rights reserved.
//#import <Foundation/Foundation.h>
#import "CarObject.h"int main(int argc, const char * argv[])
{Car *car;car = [Car new];[car print];return 0;
} // main
運行結果:
I am an engine. Vrooom!
I am a tire. I last a while.
I am a tire. I last a while.
I am a tire. I last a while.
I am a tire. I last a while.
重新設計的Car程序(復合):
//
// Car.h
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//#import <Foundation/Foundation.h>
#import "Engine.h"
#import "Tire.h"@interface Car : NSObject
{Engine *engine;Tire *tires[4];
}
- (Engine *) engine;
- (void) setEngine: (Engine *) newEngine;
- (Tire *) tireAtIndex: (int) index;
- (void) setTire: (Tire *) tire atIndex: (int) index;
- (void) print;
@end // Car //
// Car.m
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//#import "Car.h"@implementation Car
- (Engine *)engine {return (engine);
} // engine- (void) setEngine:(Engine *)newEngine {engine = newEngine;
} // setEngine:- (void) setTire:(Tire *)tire atIndex:(int)index {if (index < 0 || index > 3) {NSLog(@"bad index (%d) in setTire: atIndex:", index);exit(1);}tires[index] = tire;
} // setTire:atIndex:- (Tire *) tireAtIndex:(int)index {if (index < 0 || index > 3) {NSLog(@"bad index (%d) in tireAtIndex:", index);exit(1);}return (tires[index]);
} // tireAtIndex:- (void) print {NSLog(@"%@", engine);NSLog(@"%@", tires[0]);NSLog(@"%@", tires[1]);NSLog(@"%@", tires[2]);NSLog(@"%@", tires[3]);
} // print
@end // Car //
// Tire.h
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//#import <Foundation/Foundation.h>@interface Tire : NSObject@end //
// Tire.m
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//#import "Tire.h"@implementation Tire
- (NSString *)description {return (@"I am a tire. I last a while");
} // description
@end // Tire //
// Engine.h
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//#import <Foundation/Foundation.h>@interface Engine : NSObject@end //
// Engine.m
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//#import "Engine.h"@implementation Engine
- (NSString *)description {return (@"I am an engine. Vrooom!");
} // description
@end // Engine //
// main.m
// newCar
//
// Created by on 14-8-19.
// Copyright (c) 2014年 com.newCar. All rights reserved.
//#import <Foundation/Foundation.h>
#import "Engine.h"
#import "Tire.h"
#import "Car.h"int main(int argc, const char * argv[]) {
// new 一個car的對象Car *car = [Car new];
// new 一個engin的對象Engine *engine = [Engine new];
// 調用engin的setEngine的方法[car setEngine: engine];for (int i = 0; i < 4; i++) {Tire *tire = [Tire new];
// 調用car的setTire:atIndex:方法,并傳入兩個參數[car setTire:tire atIndex:i];}[car print];return 0;
} 運行結果:
I am an engine. Vrooom!
I am a tire. I last a while.
I am a tire. I last a while.
I am a tire. I last a while.
I am a tire. I last a while.
轉載于:https://my.oschina.net/are1OfBlog/blog/304263
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的Object-C学习代码【简单的Car程序】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。