ios构造函数

转载:http://blog.csdn.net/chaoyuan899

首先新建一个基于命令行的project,命名为“构造函数”,再新建一个Studnet类(怎样新建看前一篇),接下来是代码的编写,需要注意和说明的都写在注释里了。

Student.h

  1. //  
  2. //  Student.h  
  3. //  构造方法  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int _age;  
  14.     int _no;  
  15. }  
  16.   
  17. - (void)setAge:(int)age;  
  18. - (void)setNo:(int)no;  
  19. - (int)age;  
  20. - (int)no;  
  21. - (id)initWithAge:(int)age andNo:(int)no;  
  22. @end  

Student.m
  1. //  
  2. //  Student.m  
  3. //  构造方法  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. -(void)setAge:(int)age  
  14. {  
  15.     _age = age;  
  16. }  
  17.   
  18. - (void)setNo:(int)no  
  19. {  
  20.     _no = no;  
  21. }  
  22.   
  23. - (int)age  
  24. {  
  25.     return _age;  
  26. }  
  27.   
  28. - (int)no  
  29. {  
  30.     return _no;  
  31. }  
  32.   
  33. //实现构造方法,构造方法最好以 init 开头  
  34. - (id)initWithAge:(int)age andNo:(int)no  
  35. {  
  36.     self = [super init];//调用父类的构造方法  
  37.       
  38.     //判断self是否为nil  
  39.     if(self)  
  40.     {  
  41.         _age = age;  
  42.         _no = no;  
  43.     }  
  44.       
  45.     return self;  
  46. }  
  47.   
  48. @end  

main.m
  1. //  
  2. //  main.m  
  3. //  构造方法  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"  
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.   
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.           
  20.         [stu initWithAge:15 andNo:2];  
  21.         NSLog(@"age is %i and no is %i",[stu age], [stu no]);  
  22.           
  23.         [stu release];//Don't forget this!  
  24.     }  
  25.     return 0;  
  26. }  

    我们知道 %i 是输出整型变量,%p是输出地址,,在object c 中 %@ 很常见,它的作用是输出一个object c 对象,当用到%@时,它会调用父类的description函数,对上面的代码做如下改动:

student.h 不变

student.m 增加以下代码

  1. //%@代表打印一个对象  
  2. //当使用 %@ 打印一个对象的时候会调用这个方法,以下对这个方法进行重写  
  3. - (NSString *)description{  
  4.       
  5.     NSString *str = [NSString stringWithFormat:@"age is %i and no is %i",self.age, self.no];  
  6.     return str;  
  7. }  

main.m 增加这一句

  1. //NSLog(@"age is %i and no is %i",[stu age], [stu no]);  
  2. NSLog(@"stu %@",stu);  

输出还是跟之前的一样,如下:

2013-08-25 10:35:19.590构造方法[3042:303] stu age is 15 and no is 2


几点说明:

1.object c 变量的命名习惯是以“_”开头的,在编写变量的时候尽量遵循该习惯。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值