//// main.m// OC10变量数据类型//// Created by Zoujie on 15/9/4.// Copyright (c) 2015年 Zoujie. All rights reserved.//enum flag{ FIREST, SECOND, THIRD, FOURTH=5, FIFTH, }ENDOFDATA;//未命名的枚举类型enum { up, down, left = 10, right,};enum boolean{ no = 0, false = 0, yes = 1, ture = 1,};enum Month{ january = 1, february, march , april, may , june, july, august, september, october, november, december};typedef int Counter;//增加了变量定义的可读性typedef enum{ east, west, north, south}Direction ;#import#import "Fraction.h"typedef Fraction * NumberObject;typedef Fraction * FractionObj;int main(int argc, const char * argv[]) { @autoreleasepool { Fraction *myFtactin = [[Fraction alloc]init]; [myFtactin setTo:10 over:20]; Fraction *a = [[Fraction alloc]init]; Fraction *b = [[Fraction alloc]init]; [a setTo:1 over:3]; [b setTo:3 over:7]; [a print]; [b print]; NSLog(@"Fractions allocated :%i",[Fraction count]); a = [[Fraction allocF] init]; b = [[Fraction allocF] init]; Fraction *c = [[Fraction allocF] init]; NSLog(@"Fractions allocated :%i",[Fraction count]); //枚举// enum flag {FIREST,SECOND,THIRD}; switch (ENDOFDATA) { case FIREST: NSLog(@"first==%d",FIREST); break; case SECOND: NSLog(@"second"); break; case THIRD: NSLog(@"third"); break; case FOURTH: NSLog(@"fourth"); break; case FIFTH: NSLog(@"fifth"); break; default: break; } enum flag ONE,TWO; if (ONE){NSLog(@"ONE");}; if (TWO){NSLog(@"TWO");}; enum Month amoth; int days; NSLog(@"Enter month number:"); scanf("%i",&amoth); switch (amoth) { case january: case april: case may: case june: case july: case august: case october: case december: days = 31; break; case march: case september: case november: days = 30; break; case february: days = 28; break; default: NSLog(@"bad month number"); days = 0; break; } if (days != 0)NSLog(@"Number of days is %i",days); if (amoth == february)NSLog(@"...or 29 if it's a leap year"); Counter x = 2 , y = 3; NSLog(@"%d",x); NumberObject myValue1,myValue2,myValue3;//定义的三个Fraction对象 Direction stpe1,stpe2; switch (stpe1) { case east: NSLog(@"%i",east); break; default: break; } FractionObj f1 = [[Fraction alloc]init]; FractionObj f2 = [[Fraction alloc]init]; } return 0; }
//// Fraction.h// OC10变量数据类型//// Created by Zoujie on 15/9/4.// Copyright (c) 2015年 Zoujie. All rights reserved.//#import@interface Fraction : NSObject{ }@property (nonatomic ,assign) NSInteger a;@property (nonatomic ,assign) NSInteger b;-(void)setTo:(int)a over:(int) b;-(instancetype)initWith:(int) a over:(int) b;//自定义初始化,所有的初始化方法都以init开头-(void)print;+(Fraction *)allocF;+(int)count;@end
//// Fraction.m// OC10变量数据类型//// Created by Zoujie on 15/9/4.// Copyright (c) 2015年 Zoujie. All rights reserved.//#import "Fraction.h"static int gGlobalVar = 0; //静态变量static int gCounter;@implementation Fraction-(instancetype)init//重载init方法{ self = [super init];//首先调用父类的init方法 if (self) { NSLog(@"111111"); } return self;}-(Fraction *)initWith:(int) a over:(int) b{ self = [super init]; if (self) { [self setTo:a over:b]; } return self;}-(void)setTo:(int)a over:(int)b{ _a = a; _b = b;}-(void)print{ NSLog(@"%ld/%ld",_a,_b);}+(Fraction *)allocF{ extern int gCounter; ++gCounter; return [Fraction alloc];}+(int) count{ extern int gCounter; return gCounter;}@end