OC学习笔记01-类的声明实现与对象创建,方法声明实现与调用及传参
2015-07-23 22:46
465 查看
typedef enum{ ColorBlack='B',ColorWhite='w',ColorRed='r', ColorBlue='b',ColorGreen='g',ColorYellow='y' } Color; typedef struct{ int year,month,day; } Date; #import <Foundation/Foundation.h> //OC基本库Foundation主头文件 @interface Autobot :NSObject //类的声明,继承NSObject类,使当前类拥有父类的一些方法与属性(比如创建与销毁的方法) { //member variable必须写在{}中,类创建对象后, //这些member variables默认初始化为0,不能在interface中手动给他们赋初值 //OC中类的member variable一般都是在interface中声明,但在implementation中声明也可以 @public //public 后的所有instance variable都是公共的 int wheels ,speed; char *name; Color color; Date date;//member variable可以使任何数据类型,包括其他OC类 } //方法声明,OC中method写法,+开头为类方法,-打头为对象方法,类方法可以与对象方法同名 //OC类中方法传参方式,OC中一个方法参数对应一个:号,数据类型使用()括起来,此种方法书写方式利于代码阅读 + (NSString*)getDialogue; + (void)declare; - (void) initWithWheels:(int)wheels speed:(int)speed name:(char*)name color:(Color)color date:(Date*)date; - (void) transform; - (void)introduce; - (void)speedUp:(int)moreSpeed; - (int)compareSpeedWith:(Autobot*)anotherAutobot;//参数为OC类时必须使用指针类型,OC类只能使用指针变量来操作 @end @implementation Autobot //类的实现 //方法的实现必须放在实现类中 + (NSString*)getDialogue { return @"I AM AUTOBOT"; } + (void)declare { //类方法中self指向调用该方法的类对象,这里等同于[Autobot getDialogue] NSLog([self getDialogue]); } - (void) initWithWheels:(int)wheels speed:(int)speed name:(char*)name color:(Color)color date:(Date*)date { //初始化方法,方法参数名或局部变量名与成员变量名一样时,使用成员变量时必须使用self指向 self->wheels=wheels,self->speed=speed,self->name=name,self->color=color,self->date=*date; } - (void) transform { NSLog(@"Autobot %s transform",self->name);//OC字符串必须以@开头 //隐藏指针变量self指向当前调用该方法的类的实例 } - (void) introduce { NSLog(@"Autobot %s has %d wheels and speed is %d km/h,color is %c,made in %d-%d-%d", name,self->wheels,speed,color,date.year,date.month,date.day); //self可省略不写,默认调用的是self指向的对象的成员变量 } - (void) speedUp:(int)moreSpeed { speed+=moreSpeed; } - (int)compareSpeedWith:(Autobot*)anotherAutobot { int delta=self->speed-anotherAutobot->speed; char *p; delta>=0?(p="faster"):(p="slower"); NSLog(@"%s is %dkm/h %s than %s",name,abs(delta), p,anotherAutobot->name); return delta; } @end int main (int argc, const char *argv[]) { //OC中执行行为必须加[],格式为[行为执行者 行为名称] //OC中创建对象方式[类名 new],返回该对象内存中的指针 Autobot *p= [Autobot new]; //利用对象指针变量给对象成员变量赋值 p->wheels=8,p->speed=180,p->name="Optimus Prime",p->color=ColorBlue; Date date1={2008,10,3};p->date=date1; Autobot *p2=[Autobot new]; Date date2={2010,1,5}; //利用对象的初始化method给对象成员变量赋值 [p2 initWithWheels:4 speed:150 name:"Bumble Bee" color:ColorYellow date:&date2]; //利用对象指针变量调用对象方法 //意为给p所指向的对象发送一条introduce消息 [Autobot declare];//类方法需要通过类名调用 [p introduce],[p2 introduce]; [p transform],[p2 transform]; int delta=[p compareSpeedWith:p2]; [p2 speedUp:50]; delta=[p compareSpeedWith:p2]; /*该消息机制具体执行过程: *1>向 p所指向的对象发送一条introduce消息 *2>对象接收到消息后,调用指针变量isa所指向的类对象的introduce方法, * 并将对象自身指针以隐藏 参数的方式传给该方法的隐藏形参self * 注: 每个继承了NSObject类的对象实例中都带有一个指针变量isa, * 指向该对象的类加载后在内存中的位置; * 类加载只加载一份,加载了类中实现的方法(包括类方法与对象方法); *3>被调用的方法中根据指针变量self使用其所指向的对象的成员变量和对象方法; */ /* 类方法与对象方法异同点: *同>都是在类加载时被加载进内存,只加载一份, * 调用对象方法和类方法时,都会隐藏传递一个self指针变量指向 * 当前调用方法的对象或类对象(与java中this不同,this只有调用对象方法时才会隐式传递) *异>类方法只能由类对象调用,self指向调用它的类对象, * 对象方法只能由对象的指针调用,self指向调用它的对象, * 对象方法可以访问对象的成员变量,类方法不行; */ return 0; }
相关文章推荐
- SQL on Haoop/Spark
- 二叉堆
- UIAPPdelegate
- Memcache内存分配机制
- 在C++中实现foreach循环,比for_each更简洁!
- 《中国十亿城民——人类历史上最大规模人口流动背后的故事》:中国城市化将继续,城市对外来务工者应该更友好更包容,三星
- 《大国诸城:21世纪中国城市与区域竞争 (在这里读懂中国经济)》 比较国内重要城市、区域的经济活力,挺有意思。三星。
- 回调机制 Android
- c++引用的说明与理解
- 股票学习(技术指标02)
- 什么是光栅操作
- jquer 中常用改变display
- OS X 和 iOS 中的多线程技术
- 码农《一》
- Oracle笔记(三)单行函数
- 协议支撑
- 《SQL Server 管理与维护指南》章节目录
- Android设置背景透明度
- 多校第一场1010
- archive log 日志已满