oc构造函数和@property属性

来源:互联网 发布:plc为什么比单片机稳定 编辑:程序博客网 时间:2024/06/01 08:05


    博客分类:

oop是永恒不变的主题,无任是java还是obJect-c,只不过是语法不同而已,实质的东西相差不大,类和对象是最基础的,类事对象的组成,类由方法,属性等组成

一:java和oc创建对象:

java创建对象

     无参数构造函数

Java代码  收藏代码
  1. A a=new A();   

    java默认有一个无参数构造函数

 

     有参数构造函数 

Java代码  收藏代码
  1. A a=new A(100100);  

 

 

 

oc创建对象

    无参数构造函数

Java代码  收藏代码
  1. A* a=[[A alloc]init];  

    oc默认有一个init无参数构造函数

 

     有参数构造函数  

Java代码  收藏代码
  1. A* a =[[Aalloc] initWidthno:(100) andPhone:(100)];  

 

 

伦理片 http://www.dotdy.com/

 

二:java和oc构造函数的创建

 

     java默认构造函数

 

Java代码  收藏代码
  1. public A(){  
  2.  }  

 

 

   java有参数构造函数

   

Java代码  收藏代码
  1. int _no;  
  2. int _phone;  
  3. public A(int _no,int _phone){  
  4.    this._no-_no;  
  5.   
  6. }  

 

 

oc构造函数

   默认构造函数

   在.h文件中定义

Java代码  收藏代码
  1. -(id)init;//默认构造函数  

  在.m文件中实现

 

    

Java代码  收藏代码
  1. -(id)init{  
  2.  if(self=[super init]){  
  3.       
  4.     }  
  5.    return self;  
  6. }  

 

 

 

   有参构造函数

     .h文件中定义参数

     

Java代码  收藏代码
  1. //定义构造函数 oc默认的构造函数时init();  
  2.   
  3. //由于oc中的构造函数默认事init+大写字母   例如initWidthno  
  4. -(id)initWidthno:(int)no andPhone:(int)phone;  

 

 

    .m文件中实现.h中定义的方法

Oc代码  收藏代码
  1. //报错解决  
  2. //有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Cannot assign to 'self' outside of a method in the init family  
  3. //原因:只能在init方法中给self赋值,Xcode判断是否为init方法规则:方法返回id,并且名字以init+大写字母开头+其他  为准则。例如:- (id) initWithXXX;  
  4.   
  5. //实现构造函数  
  6. -(id)initWidthno:(int)no andPhone:(int)phone{  
  7. //    id是oc的动态加载类型,在运行时判断该类型是否存在  
  8. //     self 代表自己,类似于java的this  
  9. //    super表示父类,终于和java一样了  
  10. //    的默认构造函数时init开头,java是修饰符+类名  
  11.     if(self=[super init]){  
  12.         _phone= phone;  
  13.         _no = no;  
  14.     }  
  15.   
  16.     return self;  
  17.   
  18.      
  19. }  

 

 

 

二:@property属性的使用,个人对@property的理解是,主要用来赋值和取值的

 

  .h文件中定义

Java代码  收藏代码
  1. @interface MyStudent:NSObject{  
  2. //定义参数  
  3.     int _no;  
  4.     int _phone;  
  5.   
  6. }  
  7.   
  8. //使用@property来赋值和取参数  
  9. @property int _no;  
  10. @property int _phone;  

 

  .m文件中实现.h文件定义的参数

   

Java代码  收藏代码
  1. @synthesize _no;  
  2. @synthesize _phone;  

 

   mian文件中创建对象及调用

    

Java代码  收藏代码
  1. int main(int argc, const char * argv[]) {  
  2.     @autoreleasepool {//自动回收池  
  3.         //构造函数的使用  
  4.         MyStudent* ms =[[MyStudent alloc] initWidthno:(1111) andPhone:(2222)];  
  5.         NSLog(@"手机号码是=%d",[ms _phone]);  
  6.   
  7.         ms._no=9999;赋值  
  8.         NSLog(@"%d",[ms _no]);  
  9. }  

 

运行结果代码  收藏代码
  1. 2015-11-02 22:54:47.075 test_01[883:59225] 手机号码是=2222  
  2. 2015-11-02 22:54:47.076 test_01[883:592259999  

 

 

下面是全部完成代码:

   .h文件定义方法属性

Oc代码  收藏代码
  1. //  
  2. //  MyStudent.h  
  3. //  test_01  
  4. //  
  5. //  Created by wang on 15/11/2.  
  6. //  Copyright © 2015年 wang. All rights reserved.  
  7. //  
  8.   
  9. #ifndef MyStudent_h  
  10. #define MyStudent_h  
  11.   
  12. @interface MyStudent:NSObject{  
  13.   
  14.       
  15.     int _no;  
  16.     int _phone;  
  17.   
  18. }  
  19.   
  20. //使用@property来赋值和取参数  
  21. @property int _no;  
  22. @property int _phone;  
  23.   
  24. -(int)_no;  
  25. -(int)_phone;  
  26. //-(void)set_no:(int) newNo ;  
  27. //-(void)set_phone:(int) newPhone;  
  28.   
  29. //定义构造函数 oc默认的构造函数时init();  
  30. -(id)initWidthno:(int)no andPhone:(int)phone;  
  31.   
  32. @end  
  33.   
  34. #endif /* MyStudent_h */  

 

 影音先锋电影 http://www.iskdy.com/

   .m文件实现方法和属性

Java代码  收藏代码
  1. //  
  2. //  MyStudent.m  
  3. //  test_01  
  4. //  
  5. //  Created by wang on 15/11/2.  
  6. //  Copyright © 2015年 wang. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "MyStudent.h"  
  11.   
  12. @implementation MyStudent  
  13.   
  14. @synthesize _no;  
  15. @synthesize _phone;  
  16.   
  17.   
  18. ////由于使用了proprety属性,所以不需要使用下面赋值方法  
  19. //-(int)_no{  
  20. //    return _no;  
  21. //}  
  22. //  
  23. //-(int)_phone{  
  24. //    return _phone;  
  25. //}  
  26.   
  27. //报错解决  
  28. //有时候我们重写父类的init方法时不注意将init后面的第一个字母写成了小写,在这个方法里面又调用父类的初始化方法(self = [super init];)时会报错,错误信息如下:error:Cannot assign to 'self' outside of a method in the init family  
  29. //原因:只能在init方法中给self赋值,Xcode判断是否为init方法规则:方法返回id,并且名字以init+大写字母开头+其他  为准则。例如:- (id) initWithXXX;  
  30.   
  31. //实现构造函数  
  32. -(id)initWidthno:(int)no andPhone:(int)phone{  
  33. //    id是oc的动态加载类型,在运行时判断该类型是否存在  
  34. //     self 代表自己,类似于java的this  
  35. //    super表示父类,终于和java一样了  
  36. //    的默认构造函数时init开头,java是修饰符+类名  
  37.     if(self=[super init]){  
  38.         _phone= phone;  
  39.         _no = no;  
  40.     }  
  41.   
  42.     return self;  
  43.   
  44.      
  45. }  
  46.   
  47. @end  

 

main文件

   

Java代码  收藏代码
  1. //  
  2. //  main.m  
  3. //  test_01  
  4. //  
  5. //  Created by wang on 15/11/1.  
  6. //  Copyright © 2015年 wang. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"  
  11. #import "MyStudent.h"  
  12.   
  13. int main(int argc, const char * argv[]) {  
  14.     @autoreleasepool {//自动回收池  
  15.       //默认构造函数创建对象  
  16.        //MyStudent* ms=[[MyStudent alloc]init];  
  17.         //构造函数的使用  
  18.         MyStudent* ms =[[MyStudent alloc] initWidthno:(1111) andPhone:(2222)];  
  19.         NSLog(@"手机号码是=%d",[ms _phone]);  
  20.         ms._no=9999;  
  21.         NSLog(@"%d",[ms _no]);  
  22.           
  23.           
  24.     }  
  25.     return 0;  
  26. }  
  27.   
  28.   
  29.   
  30.       

 

0 0
原创粉丝点击