今天是入职微信的日子,早上早早的和女票一起在路边吃了个肠粉,急急忙忙坐车,然后到了TIT的时候已经十点四十了(这个确实有点晚了,貌似一般入职要在十点十分的时候)。然后HR姐姐帮我复印了材料,填好资料后,我就到了B3二楼,看到了导师sango,组长andy,和总监lyle,以上三人都很年轻,关键是还都很帅,很瘦啊。我要励志瘦成一道闪电~~。然后下午一一见过部门的各位前辈,感觉这边很忙,当然也感觉很有活力。
当然这边还有很多可写的地方,但是我还是更愿意把今天学到的一点点东西给记录下来。
今天才正式开始看OC,前几天看了看C的语法,感觉OC里面的东西和C还是比较接近的。主要看了一下OC里面的类的编写和使用。
一、语法
1、OC里面的类一般使用两个文件来描述一个类:
.h:类的声明文件,用于声明成员变量、方法。
.m:类的实现文件,用于实现在.h文件中声明的方法。
// Rectangle.h // Hello-OC // // Created by Mac on 15/11/23. // Copyright (c) 2015年 Mac. All rights reserved. // #import <Foundation/Foundation.h> @interface Rectangle : NSObject{ @private float width; float height; } //获得和设置宽度 - (float)width; - (void)setWidth:(float)newWidth; //获得和设置高度 -(float)height; -(void)setHeight:(float)newHeight; //设置宽高 -(void)setWidth:(float)newWidth andHeight:(float)newHeight; //获得面积 -(float)area; @end
如上是一个类的头文件。
// Rectangle.m // Hello-OC // // Created by Mac on 15/11/23. // Copyright (c) 2015年 Mac. All rights reserved. // #import "Rectangle.h" @implementation Rectangle //获得和设置宽度 -(float)width{ return width; } -(void)setWidth:(float)newWidth{ width=newWidth; } //获得和设置高度 -(float)height{ return height; } -(void)setHeight:(float)newHeight{ height=newHeight; } -(void)setWidth:(float)newWidth andHeight:(float)newHeight{ width=newWidth; height=newHeight; } -(float)area{ return width*height; } @end
如上是一个类的具体实现。
然后下面是OC中类的使用方法:
#import <Foundation/Foundation.h> #import "Rectangle.h" int main(int argc, const char * argv[]) { @autoreleasepool { Rectangle *rect=[[Rectangle alloc]init]; Rectangle *rect2=[Rectangle new]; [rect setWidth:100 andHeight:50]; [rect2 setHeight:79.2f]; NSLog(@"the width of rect is %.3f",[rect width]); NSLog(@"the width of rect2 is %.2f",[rect2 height]); } return 0; }
我们可以看到OC的语法确实还是比较奇特的,和Java的语法有很大的不同。具体的不同会在下篇文章中做具体的分析