首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

objective-c中的构造函数(对象初始化)(7)

2013-10-16 
objective-c中的构造函数(对象初始化)(七)holydancer原创,如需转载,请在显要位置注明:转自holydancer的CSD

objective-c中的构造函数(对象初始化)(七)

holydancer原创,如需转载,请在显要位置注明:

转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7354489

?

? ? ? ?以前我们创建对象时都是用new,从java过来的同学也都有这种习惯,其实objective-c中还有一种地道的创建对象的方法:[ [类名 alloc ] init].这种方法才是OC中创建对象的正统,不过效果和new是一样的,但是这种方法更能表示创建对象的实质,那就是分配内存,初始化对象。

alloc是在内存中划分一片空间,这片空间是一片处女地,然后呢,init初始化,我们可以在初始化的时候设置一些基本属性的值,这样就不用在创建对象后再调用方法来赋值。看代码:

human.h:

?

[plain]?view plaincopy?
  1. #import?<Foundation/Foundation.h>??
  2. ??
  3. @interface?Human?:?NSObject??
  4. {??
  5. ????int?age;??
  6. ????NSString?*name;??
  7. }??
  8. -(void)setAge:(int)a?setName:(NSString?*)n;??
  9. -(int)age;??
  10. -(NSString?*)name;??
  11. @end??


human.m:

?

?

[plain]?view plaincopy?
  1. #import?"Human.h"??
  2. ??
  3. @implementation?Human??
  4. -(id)init??
  5. {??
  6. ????if(self=[super?init])??
  7. ????{??
  8. ????????age=20;??
  9. ????????name=@"holy";??
  10. ????}??
  11. ????return?self;??
  12. }??
  13. -(void)setAge:(int)a?setName:(NSString?*)n??
  14. {??
  15. ????age=a;??
  16. ????[name?release];??
  17. ??????
  18. ????name=[n?copy];??
  19. }??
  20. -(int)age??
  21. {??
  22. ????return?age;??
  23. }??
  24. -(NSString?*)name??
  25. {??
  26. ????return?name;??
  27. }??
  28. @end??


main.m:

?

?

[plain]?view plaincopy?
  1. #import?<Foundation/Foundation.h>??
  2. #import?"Human.h"??
  3. int?main(int?argc,?const?char?*?argv[])??
  4. {??
  5. ????NSAutoreleasePool?*pool;??
  6. ????pool?=[[NSAutoreleasePool?alloc]?init];??
  7. ??????
  8. ????Human?*?human?=?[[Human?alloc]?init];??
  9. ????NSLog(@"名字%@,年龄%d",[human?name],[human?age]);??
  10. ??????
  11. ????[human?setAge:100?setName:@"GOD"];??
  12. ????NSLog(@"名字%@,年龄%d",[human?name],[human?age]);??
  13. ??
  14. ????[human?release];??
  15. ????[pool?release];//相当于对池中每个对象执行了一次release;??
  16. ??????
  17. ??
  18. }??

?

?

上面的代码,在其中有一行这种初始化方法很好理解,但是有个缺点就是生成对象的默认属性值是固定的,如果想要修改的话需要再调用[plain]?view plaincopy?

  1. #import?<Foundation/Foundation.h>??
  2. ??
  3. @interface?Human?:?NSObject??
  4. {??
  5. ????int?age;??
  6. ????NSString?*name;??
  7. }??
  8. -(id)initWithAge:(int)a?Name:(NSString?*)n;??
  9. -(int)age;??
  10. -(NSString?*)name;??
  11. @end??


Human.m:

?

?

[plain]?view plaincopy?
  1. #import?"Human.h"??
  2. ??
  3. @implementation?Human??
  4. -(id)init??
  5. {??
  6. ????if(self=[super?init])??
  7. ????{??
  8. ????????age=20;??
  9. ????????name=@"holy";??
  10. ????}??
  11. ????return?self;??
  12. }??
  13. -(id)initWithAge:(int)a?Name:(NSString?*)n??
  14. {??
  15. ????if?(self=[super?init])?{??
  16. ????????age=a;??
  17. ????????[name?release];??
  18. ??????????
  19. ????????name=[n?copy];??
  20. ????}??
  21. ????return?self;??
  22. ?????
  23. }??
  24. -(int)age??
  25. {??
  26. ????return?age;??
  27. }??
  28. -(NSString?*)name??
  29. {??
  30. ????return?name;??
  31. }??
  32. @end??


main.m:

?

?

[plain]?view plaincopy?
  1. #import?<Foundation/Foundation.h>??
  2. #import?"Human.h"??
  3. int?main(int?argc,?const?char?*?argv[])??
  4. {??
  5. ????NSAutoreleasePool?*pool;??
  6. ????pool?=[[NSAutoreleasePool?alloc]?init];??
  7. ??????
  8. ????Human?*?human1?=?[[Human?alloc]?init];??
  9. ????NSLog(@"名字%@,年龄%d",[human1?name],[human1?age]);??
  10. ??????
  11. ????Human?*?human2?=?[[Human?alloc]?initWithAge:100?Name:@"GOD"];??
  12. ????NSLog(@"名字%@,年龄%d",[human2?name],[human2?age]);??
  13. ??????
  14. ??????
  15. ????[human1?release];??
  16. ????[human2?release];??
  17. ????[pool?release];//相当于对池中每个对象执行了一次release;??
  18. ??????
  19. ??
  20. }??


输出:

?

?

[plain]?view plaincopy?
  1. 2012-03-14?21:03:24.974?String[460:403]?名字holy,年龄20??
  2. 2012-03-14?21:03:24.976?String[460:403]?名字GOD,年龄100??
[plain]?view plaincopy?
  1. 关键字:objective-c?,objective?c?,?oc?,构造函数?,对象初始化?
?

热点排行