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
相关推荐
Retain,copy,assign...
OC-模型的Setter,Getter的由来,@property的由来
OC函数大全 所有的OC方法全部包含在内 OC开发必备文档
@property (nonatomic, assign) CGFloat ocWidth; @property (nonatomic, assign) CGFloat ocHeight; @property (nonatomic, assign) CGFloat ocLeft; @property (nonatomic, assign) CGFloat ocTop; @property ...
@property (nonatomic, copy) NSString *placeholder; /** 占位文字的颜色 */ @property (nonatomic, strong) UIColor *placeholderColor; /** 占位文字的大小 */ @property (nonatomic, strong) UIFont *...
oc TextField 键盘弹出移动位置 //上移后,textField需要额外高于键盘...@property (nonatomic, assign) CGFloat offset; //需要向上移动的view,默认为keyWindow @property (nonatomic, weak) UIView *movingView;
让oc像swift那样给@protocol协议添加方法的默认实现教程加demo,例子里面实现了xib文件,遵守协议以后直接就能被调用的例子
oc宝典 oc宝典 oc宝典 oc宝典 oc宝典 oc宝典oc宝典 oc宝典 oc宝典
基于OC函数的抽检方案及其风险控制,王三宝,易秀英,质量控制是一系列的检查和测量来确定质量水平是否达到要求,抽样检验是质量控制的重要手段之一,它是从一批产品或一个过程中抽取
OC-@synchronized
@property (nonatomic,copy) YSCountDownBlock block; - (void)destoryTimer; ///每秒走一次,回调block dataList 倒计时时间戳(未来的结束时间) canReloadList 是否允许刷新(本身进来活动结束的商品是不能刷新的)...
关于oc的归档代码 @implementation Twitter void encodeWithCoder: NSCoder aCoder{ [aCoder encodeObject: comment forKey:@"comment"]; [aCoder encodeObject: content forKey:@"content"...
(OC)Setter,Getter修改私有属性
资源名称:渥瑞达OC培训视频教程(39节)资源目录:【】OC第1讲01-oc语言及开发环境介绍【】OC第1讲02-...OC第2讲03-属性声明【】OC第2讲04-属性声明【I 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
lua与oc的交互demo,封装了一层luaBridge,开源lua库,lua调用oc的过程
ios oc手动代码混淆函数名和变量名demo,包含注意事项的举例
[Swift]1.在OC项目里的OC代码调用Swift方法,Swift代码里调用OC代码。2.在Swift项目里的Swift代码里调用OC代码,OC代码调用Swift方法。
NSString *str=@"fsdggfhjtyhdrfsdh"; //单个删除 NSString *str1=[str substringFromIndex:1]; NSLog(@"%@",str1); //选择删除 NSString *str2=[str substringWithRange:NSMakeRange(0, 3)];...
Swift和OC混编,OC项目下Swift和OC的互相调用,Swift项目下Swift和OC的相互调用!