练习代码:
代码一:(面向过程的OC)
// // main.m // Class01Test01 // // Copyright (c) 2014年 levi. All rights reserved. // #import <Foundation/Foundation.h> @interface Fraction : NSObject -(void) setarr; -(void) rand; -(void) judge; @end @implementation Fraction { NSInteger a; NSInteger b; //NSArray *arr = @[@"剪刀", @"石头", @"布"]; NSArray *arr; } -(void)setarr { arr = @[@"剪刀",@"石头",@"布"]; } -(void)rand { a = arc4random()%3; b = arc4random()%3; } -(void)judge{ NSLog(@"%@ vs %@", arr[a], arr[b]); if( (a == 0 && b == 2) || a > b) NSLog(@"%@ vs %@ A胜出", arr[a], arr[b]); else if(a == b) NSLog(@"%@ vs %@ 平局", arr[a], arr[b]); else if( (a == 2 && b == 0) || a < b ) NSLog(@"%@ vs %@ B胜出", arr[a], arr[b]); else NSLog(@"ERROR!"); } @end int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... //常量字符串 //@“”字符串对象 //0 1 2 j s b Fraction *frac1 = [[Fraction alloc] init]; [frac1 setarr]; [frac1 rand]; [frac1 judge]; } return 0; }
猜拳代码(简单面向对象的思想):
// // Guess.h // Class1-02 // // Copyright (c) 2014年 levi. All rights reserved. // #import <Foundation/Foundation.h> /* 代码规范: 1.类名首字母大写 2.方法名首字母小写 类:抽象 笔记本 对象:具体 桌上这台笔记本 oc中声明一个类 @interface开始 Guess(类名): NSObject(父类) @end结束 oc中所有类都直接或者间接的继承自 NSObject */ @interface Guess : NSObject //void startGuess; -(void) startGuess; /* OC中方法 “-” 对象方法、对象调用 “+” 类方法、类调用 startGuess 方法名 */ @end
// // Guess.m // Class1-02 // // Copyright (c) 2014年 levi. All rights reserved. // #import "Guess.h" /* @implementation 实现 方法1 实现 方法2 实现 @end 结束 */ @implementation Guess - (id)init { /* 定时器、每隔一秒会调用当前对象的startGuess方法 */ self = [super init]; if (self) { [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startGuess) userInfo:nil repeats:YES]; } return self; } -(void)startGuess { NSArray *arr = @[@"剪刀", @"石头", @"布"]; NSInteger a = arc4random()%3; NSInteger b = arc4random()%3; NSLog(@"%@ vs %@", arr[a], arr[b]); if( (a == 0 && b == 2) || a > b) NSLog(@"%@ vs %@ A胜出", arr[a], arr[b]); else if(a == b) NSLog(@"%@ vs %@ 平局", arr[a], arr[b]); else if( (a == 2 && b == 0) || a < b ) NSLog(@"%@ vs %@ B胜出", arr[a], arr[b]); else NSLog(@"ERROR!"); } @end
// // main.m // Class1-02 // // Copyright (c) 2014年 levi. All rights reserved. // #import <Foundation/Foundation.h> #import "Guess.h" int main(int argc, const char * argv[]) { @autoreleasepool { /* 语法[类名/方法名]; 类经过实例化 变成对象 char *p = malloc(1024); memset(p, 0, 1024); */ Guess *g = [Guess alloc]; g = [g init]; [g startGuess]; /* 启动事件循环 让main函数不退出 */ [[NSRunLoop currentRunLoop] run]; } return 0; }
简单的继承:
// // Person.h // Class1_03 // // Copyright (c) 2014年 levi. All rights reserved. // #import <Foundation/Foundation.h> @interface Person : NSObject { /* 成员变量(字段) 命名规范:_xxx mxxx @public 公共类型 @和C++做区分 @private 私有类型 只能在本类中访问 @proteted 保护类型 能在当前类和它的子类中访问(缺省) */ @protected NSInteger _age; BOOL _gender; CGFloat _height; } /* 方法声明(+ -) */ - (void)qfTest; //+ (void)qfTestone; //带参数 - (void)setAge:(NSInteger)age; - (NSInteger)age; - (void)setGender:(BOOL)gender; - (BOOL)gender; - (void)setHeight:(CGFloat)height; - (CGFloat)height; - (void)setAge:(NSInteger)age withGender:(BOOL)gender; /* 方法名 : setAge:withGender */ @end // // Person.m // Class1_03 // // Copyright (c) 2014年 levi. All rights reserved. // #import "Person.h" @implementation Person -(void)qfTest { _age = 10; } /* +方法无法访问成员变量 +(void)qfTestone { _age = 10; } */ - (void)setAge:(NSInteger)age { _age = age; } - (NSInteger)age { return _age ; } - (void)setGender:(BOOL)gender { _gender = gender; } - (BOOL)gender { return _gender; } - (void)setHeight:(CGFloat)height { _height = height; } - (CGFloat)height { return _height; } - (void)setAge:(NSInteger)age withGender:(BOOL)gender { _age = age; _gender = gender; } @end // // Student.h // Class1_03 // // Copyright (c) 2014年 levi. All rights reserved. // #import "Person.h" @interface Student : Person @end // // Student.m // Class1_03 // // Copyright (c) 2014年 levi. All rights reserved. // #import "Student.h" @implementation Student -(void)test { /* self 当前类或者当前对象 super 当前类或者对象的父类 */ self->_age = 90; } @end // // main.m // Class1_03 // // Copyright (c) 2014年 levi. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" #import "Student.h" int main(int argc, const char * argv[]) { @autoreleasepool { Person *p = [[Person alloc] init]; [p qfTest]; /* [Person qfTestone]; + 方法只能类调用 - 方法只能对象调用 */ /* @public 可以访问 p->_age = 24; p->_gender = YES; p->_height = 1.82; NSLog(@"Age is %ld\nGender is %d\nHeight is %g", p->_age, p->_gender, p->_height); */ //Student *s = [[Student alloc] init]; /* 以方法形式暴露成员变量 */ [p setAge:99 withGender:YES]; [p setGender:NO]; //[p setHeight:18.2]; NSLog(@"p->_age is %ld\np->_gender is %d\np->height = %lf", [p age], [p gender], [p height]); } return 0; }
题目:小明手里有两张牌,左手红桃A,右手方片K,现在让小明交换两只手里的牌,问交换之后,小明左右手的牌,各是什么?
用面向对象的方式来说明这个题目
代码如下:
// // XiaoMing.h // Class1_04 // // Created by Levi on 14-3-13. // Copyright (c) 2014年 levi. All rights reserved. // #import <Foundation/Foundation.h> #import "Hand.h" @interface XiaoMing : NSObject { Hand *_left; Hand *_right; } - (void) setLeftCard:(char)card1 rightCard:(char)card2; - (void) swap; - (void) result; @end // // XiaoMing.m // Class1_04 // // Created by Levi on 14-3-13. // Copyright (c) 2014年 levi. All rights reserved. // #import "XiaoMing.h" @implementation XiaoMing - (void) setLeftCard:(char)card1 rightCard:(char)card2 { _left = [[Hand alloc] init]; _right = [[Hand alloc] init]; [_left setCard:card1]; [_right setCard:card2]; } - (void) swap { char tmp = [_left card]; [_left setCard:[_right card]]; [_right setCard:tmp]; } - (void) result { NSLog(@"左手 : %c,右手 : %c", [_left card], [_right card]); } @end // // Hand.h // Class1_04 // // Created by Levi on 14-3-13. // Copyright (c) 2014年 levi. All rights reserved. // #import <Foundation/Foundation.h> @interface Hand : NSObject { char _card; } - (void)setCard:(char)card; - (char)card; @end // // Hand.m // Class1_04 // // Created by Levi on 14-3-13. // Copyright (c) 2014年 levi. All rights reserved. // #import "Hand.h" @implementation Hand - (void)setCard:(char)card { _card = card; } - (char)card { return _card; } @end // // main.m // Class1_04 // // Created by Levi on 14-3-13. // Copyright (c) 2014年 levi. All rights reserved. // #import <Foundation/Foundation.h> #import "XiaoMing.h" int main(int argc, const char * argv[]) { @autoreleasepool { XiaoMing *xm = [[XiaoMing alloc] init]; [xm setLeftCard:'A' rightCard:'B']; [xm result]; [xm swap]; [xm result]; } return 0; }
Ps:临渊羡鱼、不如退而结网。