A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© wowthe1st 中级黑马   /  2015-7-30 14:03  /  675 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

适合有java经验的同学们快速了解OC这门语言,个人习惯用代码做笔记,比较直观

  1. typedef enum{
  2.         ColorBlack='b',ColorYellow='y',ColorWhite='w'
  3. } Color;

  4. #import<Foundation/Foundation.h>

  5. @interface Pet : NSObject
  6. {
  7.         @private
  8.         int _age;
  9.         double _weight;
  10.         NSString * _name;
  11. }

  12. - (id)initWithAge:(int)age andWeight:(double)weight andName:
  13. (NSString *)name;

  14. -(id)initTest;

  15. - (NSString *)toString;


  16. @end

  17. @implementation Pet

  18. // 重写init方法,由于是无参构造方法,无法动态给成员变量赋值
  19. - (id)init  
  20. {
  21.         if(self=[super init]) //构造方法中必须调用父类构造方法
  22.         {
  23.                 _age=1,_weight=1.0,_name=@"pet";
  24.         }
  25.         return self;
  26. }

  27. //自定义构造方法,可以自定义参数,灵活的给成员变量赋值
  28. - (id)initWithAge:(int)age andWeight:(double)weight andName:(NSString *)name
  29. {
  30.         if(self=[super init])
  31.         {
  32.                 _age=age,_weight=weight,_name=name;
  33.         }
  34.         return self;
  35. }

  36. -(id)initTest
  37. {
  38.         if(self=[super init])
  39.         {
  40.                 //从结果可知,[super class],[self class]都是指向调用者的类对象
  41.                 // 即若由Dog的实例调用,则都指向Dog的类对象,即[Dog class];
  42.                 NSLog(@"super=%d,self=%d,superclass=%d,selfclass=%d,Pet=%d",
  43.                 super,self,[super class],[self class],[Pet class]);
  44.         }
  45.         return self;
  46. }

  47. - (NSString *)toString
  48. {
  49.         return [NSString stringWithFormat:@"name=%@ ,weight=%.2f ,age=%d",
  50.         _name,_weight,_age];
  51. }

  52. @end

  53. @interface Dog : Pet
  54. {
  55.         @private
  56.         Color _furColor;
  57. }

  58. - (id)initWithAge:(int)age andWeight:(double)weight andName:
  59. (NSString *)name andFurColor:(Color)furColor;
  60. @end
  61. @implementation Dog

  62. - (id)init  
  63. {
  64.         if(self=[super init])
  65.         {
  66.                 _furColor=ColorYellow;
  67.         }
  68.         return self;
  69. }

  70. - (id)initWithAge:(int)age andWeight:(double)weight andName:
  71. (NSString *)name andFurColor:(Color)furColor
  72. {
  73.         //调用父类的有参构造方法给父类成员赋值
  74.         if(self=[super initWithAge:age andWeight:weight andName:name])
  75.         {
  76.                 _furColor=furColor;
  77.         }
  78.         return self;
  79. }

  80. -(id)initTest
  81. {
  82.         if(self=[super initTest])
  83.         {
  84.                 NSLog(@"Dog=%d",[Dog class]);
  85.         }
  86.         return self;
  87. }

  88. - (NSString *)toString
  89. {
  90.         return [NSString stringWithFormat:@"%@ ,furColor=%c",
  91.         [super toString],_furColor];
  92. }

  93. @end


  94. int main()
  95. {
  96.         //id作为万能指针,能指向任意OC对象
  97.         id *p=[[Dog alloc] init];
  98.         NSLog(@"this dog is %@",[p toString]);
  99.         id *p2=[[Dog alloc] initWithAge:4 andWeight:12.2 andName:@"Daisy"
  100.         andFurColor:ColorWhite];
  101.         NSLog(@"this dog is %@",[p2 toString]);
  102.         id *p3=[[Dog alloc] initTest];
  103.         NSLog(@"p3=%d",p3);
  104.         return 0;
  105. }




  106. /*

  107. id关键字:
  108. typedef struct objc_object {
  109.          Class isa;
  110. } *id;


  111. //构造对象相关方法伪代码
  112. + (id)new
  113. {
  114.         return [[self alloc] init];
  115. }

  116. + (id)alloc
  117. {
  118.         id obj=(struct objc_object)malloc(父类到子类所有成员变量所占字节数总和);
  119.         return obj;
  120. }

  121. - (id)init
  122. {
  123.         //[self class]返回类指针指向当前对象的类对象
  124.         isa=[self class];
  125.         return self;
  126. }


  127. //重写init方法必须调用[super init]来初始化父类中的成员变量  ---类似java无参constructor
  128. - (id)init
  129. {
  130.        
  131.         if(self=[super init])
  132.         {
  133.                 ..初始化操作..
  134.         }
  135.         return self;
  136.        
  137. }


  138. //自定义构造方法(一般以init开头)  ---类似java有参构造器
  139. - (id)initWithName:(NSString *)name
  140. {
  141.         if(self=[super init])
  142.         {
  143.                 _name=name;
  144.         }
  145.         return self;
  146. }

  147. */
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马