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
相关推荐
OC中,`@property`关键字用于声明属性,而`@synthesize`则自动生成对应的setter和getter方法。在XCode5.0以后,`@synthesize`通常是自动完成的。 属性分为实例变量和类变量。实例变量以下划线`_`开头,若想在类外部...
总之,iOS开发中的OC面向对象编程涉及类、对象、属性、方法、构造函数、析构函数等多个方面,而点语法、内存管理和消息传递则是其独特之处。理解并熟练掌握这些知识点对于iOS开发者来说至关重要。在实际开发中,还...
alloc是分配内存,对象计数器为1 init是初始化工作 类似于c++构造函数 3. 请问 property 的 作用,assign,copy,retain 的 区别; assign就是基本赋值 copy是重新创建一个oc对象,计数器是1 retain是对计数器+...
// 如果需要暴露结构体的构造函数或方法,也可以在这里声明 } ``` 3. 使用C++结构体:在Objective-C的.m文件中,我们可以引入桥接头文件并直接使用C++结构体。对于Swift,需要在项目设置中开启“Use C++ Language ...
方法的概念,包括实例方法和类方法,以及构造函数和自定义构造函数的使用,都是面向对象程序设计的重要组成部分。Objective-C中封装的概念尤为重要,其中setter和getter方法的建立以及property关键字的使用都是关键...
- **对比分析:** 构造不是面向对象编程的基本特性,构造主要指类的构造函数或初始化方法,用于创建类的实例。 #### 十二、所有类的根类 **知识点:** 根类 - **背景介绍:** 在Objective-C中,所有类最终都继承自`...
与Category类似,但可以添加计算属性、实例方法、构造函数等。 - **继承**:允许一个类继承另一个类的属性和方法,可以覆盖父类的方法,也可以添加新的方法。 **8. 为什么代理要用 weak?** 使用`weak`来声明...