ObjectiveC - 类的声明和实现(一)

来源:互联网 发布:淘宝 花 编辑:程序博客网 时间:2024/05/18 02:38

类的声明 — 类名.h

     @interface Person : NSObject     {          /*实例变量*/          int age;          int identify;          // 与java不同,实例变量不能在这里初始化     }     /*方法声明*/     /**        - (void) main:(String) args       + ……       - 表示实例方法, + 表示类方法(静态方法)       (void) 返回类型       main: 方法名,注意这个冒号       (String) args 参数     */     - (void) setAge:(int) _age;     - (int) getAge;     // oc方法声明 之 怪异声明方式     - (id) initWithAge:(int) _age identify:(int) identify;     @end
类的实现 — 类名.m

     #import "Person.h"     @implementation Person     // 初始化方法,类似Java的构造方法     -(id) initWithAge:(int)_age identify:(int)_identify     {          if (self = [super init]) {               age = _age;               identify = _identify;          }          return self;     }     -(int) getAge     {          return age;     }     -(int) getIdentify     {          return identify;     }     -(void) setAge:(int)_age     {          age = _age;     }     @end