oop是永恒不变的主题,无任是java还是obJect-c,只不过是语法不同而已,实质的东西相差不大,类和对象是最基础的,类事对象的组成,类由方法,属性等组成
一:java和oc创建对象:
java创建对象
? ? ?无参数构造函数
A a=new A();
? ??java默认有一个无参数构造函数
?
? ? ?有参数构造函数?
A a=new A(100,100);
?
?
?
oc创建对象
? ? 无参数构造函数
A* a=[[A alloc]init];
? ??oc默认有一个init无参数构造函数
?
? ? ?有参数构造函数??
A* a =[[Aalloc] initWidthno:(100) andPhone:(100)];
?
?
?
二:java和oc构造函数的创建
?
? ? ?java默认构造函数
?
public A(){ }
?
?
? ?java有参数构造函数
? ?
int _no; int _phone; public A(int _no,int _phone){ this._no-_no; }
?
?
oc构造函数
? ?默认构造函数
? ?在.h文件中定义
-(id)init;//默认构造函数
? 在.m文件中实现
?
? ??
-(id)init{ if(self=[super init]){ } return self; }
?
?
?
? ?有参构造函数
? ? ?.h文件中定义参数
? ? ?
//定义构造函数 oc默认的构造函数时init(); //由于oc中的构造函数默认事init+大写字母 例如initWidthno -(id)initWidthno:(int)no andPhone:(int)phone;
?
?
? ? .m文件中实现.h中定义的方法
//报错解决 //有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Cannot assign to ‘self‘ outside of a method in the init family //原因:只能在init方法中给self赋值,Xcode判断是否为init方法规则:方法返回id,并且名字以init+大写字母开头+其他 为准则。例如:- (id) initWithXXX; //实现构造函数 -(id)initWidthno:(int)no andPhone:(int)phone{ // id是oc的动态加载类型,在运行时判断该类型是否存在 // ?self 代表自己,类似于java的this // super表示父类,终于和java一样了 // 的默认构造函数时init开头,java是修饰符+类名 if(self=[super init]){ _phone= phone; _no = no; } return self; }
?
?
?
二:@property属性的使用,个人对@property的理解是,主要用来赋值和取值的
?
? .h文件中定义
@interface MyStudent:NSObject{ //定义参数 int _no; int _phone; } //使用@property来赋值和取参数 @property int _no; @property int _phone;
?
? .m文件中实现.h文件定义的参数
? ?
@synthesize _no; @synthesize _phone;
?
? ?mian文件中创建对象及调用
? ??
int main(int argc, const char * argv[]) { @autoreleasepool {//自动回收池 //构造函数的使用 MyStudent* ms =[[MyStudent alloc] initWidthno:(1111) andPhone:(2222)]; NSLog(@"手机号码是=%d",[ms _phone]); ms._no=9999;赋值 NSLog(@"%d",[ms _no]); }
?
2015-11-02 22:54:47.075 test_01[883:59225] 手机号码是=2222 2015-11-02 22:54:47.076 test_01[883:59225] 9999
?
?
下面是全部完成代码:
? ?.h文件定义方法属性
// // MyStudent.h // test_01 // // Created by wang on 15/11/2. // Copyright ? 2015年 wang. All rights reserved. // #ifndef MyStudent_h #define MyStudent_h @interface MyStudent:NSObject{ int _no; int _phone; } //使用@property来赋值和取参数 @property int _no; @property int _phone; -(int)_no; -(int)_phone; //-(void)set_no:(int) newNo ; //-(void)set_phone:(int) newPhone; //定义构造函数 oc默认的构造函数时init(); -(id)initWidthno:(int)no andPhone:(int)phone; @end #endif /* MyStudent_h */
?
?
? ?.m文件实现方法和属性
// // MyStudent.m // test_01 // // Created by wang on 15/11/2. // Copyright ? 2015年 wang. All rights reserved. // #import <Foundation/Foundation.h> #import "MyStudent.h" @implementation MyStudent @synthesize _no; @synthesize _phone; ////由于使用了proprety属性,所以不需要使用下面赋值方法 //-(int)_no{ // return _no; //} // //-(int)_phone{ // return _phone; //} //报错解决 //有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Cannot assign to ‘self‘ outside of a method in the init family //原因:只能在init方法中给self赋值,Xcode判断是否为init方法规则:方法返回id,并且名字以init+大写字母开头+其他 为准则。例如:- (id) initWithXXX; //实现构造函数 -(id)initWidthno:(int)no andPhone:(int)phone{ // id是oc的动态加载类型,在运行时判断该类型是否存在 // ?self 代表自己,类似于java的this // super表示父类,终于和java一样了 // 的默认构造函数时init开头,java是修饰符+类名 if(self=[super init]){ _phone= phone; _no = no; } return self; } @end
?
main文件
? ?
// // main.m // test_01 // // Created by wang on 15/11/1. // Copyright ? 2015年 wang. All rights reserved. // #import <Foundation/Foundation.h> #import "Student.h" #import "MyStudent.h" int main(int argc, const char * argv[]) { @autoreleasepool {//自动回收池 //默认构造函数创建对象 //MyStudent* ms=[[MyStudent alloc]init]; //构造函数的使用 MyStudent* ms =[[MyStudent alloc] initWidthno:(1111) andPhone:(2222)]; NSLog(@"手机号码是=%d",[ms _phone]); ms._no=9999; NSLog(@"%d",[ms _no]); } return 0; }
?
运行结果:
2015-11-02 23:10:42.460 test_01[972:67976] 手机号码是=2222
?
2015-11-02 23:10:42.461 test_01[972:67976] 9999
?
?
?
原文:http://baihe747.iteye.com/blog/2253938