关于Objective-C构造函数举例

移动开发 iOS
Objective-C构造函数举例是本文要介绍的内容,主要是来学习Objective-C中的构造函数,本文呢内容不多,驻澳是基于代码实现,来看详细内容。

Objective-C构造函数举例是本文要介绍的内容,主要是来学习Objective-C中的构造函数,本文呢内容不多,驻澳是基于代码实现,来看详细内容。

  1. #include  <Foundation/Foundation.h> 
  2. #include  <stdio.h> 
  3.  
  4. @interface  Container:NSObject  
  5. {  
  6.  int number;  
  7. }  
  8. -(void) setNumber:(int) n;  
  9. -(int) intValue;  
  10. -(Container*) init:(int)n;  
  11. @end;  
  12.  
  13. @implementation Container  
  14. -(void) setNumber:(int) n  
  15. {  
  16.  nnumber=n;  
  17. }  
  18. -(int) intValue  
  19. {  
  20.  return number;  
  21. }  
  22.  
  23. //构造函数  
  24. -(Container*)init:(int) n  
  25. {  
  26.  self=[super  init];  
  27.  if (self){  
  28.  [self setNumber:n];  
  29. }  
  30.  
  31. return self;  
  32. }  
  33. @end  
  34. int main(void)  
  35. {  
  36.  Container  *object=[[Container new]   init : 3];  
  37.  printf( "The number is %i\n",  [object intValue]);  
  38.  return 0;  

输出结果:

  1. The number is 3 

小结:关于Objective-C构造函数举例的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-04 11:15:46

Objective-C 构造函数 构造方法

2011-08-17 10:58:59

Objective-C构造函数

2011-07-20 13:34:37

Objective-C self.

2011-08-15 14:02:36

Objective-C

2011-08-17 15:37:23

Objective-C垃圾收集

2011-08-01 17:11:43

Objective-C 函数

2011-08-04 15:14:39

Objective-C 数据类型

2011-08-10 18:07:29

Objective-C反射

2011-08-04 16:46:03

Objective-C 声明

2013-03-27 12:54:00

iOS开发Objective-C

2013-06-20 10:40:32

Objective-C实现截图

2011-05-11 11:20:26

Objective-C

2011-05-11 15:58:34

Objective-C

2011-08-04 14:58:37

Objective-C Cocoa NSString

2011-05-11 13:54:08

Objective-C

2011-05-11 15:45:50

内存管理Objective-C

2011-08-02 13:16:36

Objective-C 语法 函数

2011-05-11 14:06:49

Objective-C

2013-08-21 14:57:42

objective-c问题

2014-04-30 10:16:04

Objective-CiOS语法
点赞
收藏

51CTO技术栈公众号