1、类和方法

下图中是一段的类声明的语法展示,声明了一个叫做 MyClass 的类,它继承于根类:NSObject。(根类可以被所有的其他类直接或间接继承。)

Objective-C语法之类和对象_深入浅出Objective-C

 

下图是一个方法的语法展示,方法的声明由以下几个部分构成:方法类型标识符,返回类型,一个或多个方法签名关键字,以及参数类型和名称。

Objective-C语法之类和对象_成员变量_02

 

类的实体变量的访问权限:

Objective-C语法之类和对象_构造函数_03

对应的代码:

 



[cpp]  ​​view plain​​​ ​​​copy​



  1. @interface Worker : NSObject
  2. {
  3. char *name;
  4. @private
  5. int age;
  6. char *evaluation;
  7. @protected
  8. id job;
  9. float wage;
  10. @public
  11. id boss;
  12. }



没有写关键字的实体变量,比如 char *name是@protected的。也就是说,默认是protected的。

 

2、创建类

1.1、新建版项目,按Command + N 新建文件,创建类Student ,继承与NSObject

 

 

1.2、生成student.h  和student.m



[cpp]  ​​view plain​​​ ​​​copy​



  1. #import <Foundation/Foundation.h>

  2. @interface Student : NSObject

  3. @end



[cpp]  ​​view plain​​​ ​​​copy​



  1. #import "Student.h"

  2. @implementation Student

  3. @end



 

1.3、在头文件里添加类成员变量和方法

@interface

 



[cpp]  ​​view plain​​​ ​​​copy​



  1. #import <Foundation/Foundation.h>

  2. @interface Student : NSObject
  3. {
  4. NSString *studentName;
  5. NSInteger age;
  6. }

  7. -(void) printInfo;
  8. -(void) setStudentName: (NSString*) name;
  9. -(void) setAge: (NSInteger) age;
  10. -(NSString*) studentName;
  11. -(NSInteger) age;
  12. @end



 

 

  • @interface 类的开始的标识符号 ,好比Java  或 C 语言中的Class
  • @end 类的结束符号
  • 继承类的方式:Class:Parent,如上代码Student:NSObject
  • 成员变量在@interface Class: Parent { .... }之间
  • 成员变量默认的访问权限是protected。
  • 类成员方法在成员变量后面,格式是:: scope (returnType) methodName: (parameter1Type) parameter1Name;
  • scope指得是类方法或实例化方法。类方法用+号开始,实例化方法用 -号开始。

 

1.4、实现类中的方法

@implementation

 



[cpp]  ​​view plain​​​ ​​​copy​



  1. #import "Student.h"

  2. @implementation Student

  3. -(void) printInfo
  4. {
  5. "姓名:%@ 年龄:%d岁",studentName,studentAge);
  6. }
  7. -(void) setStudentName: (NSString*) name
  8. {
  9. studentName = name;
  10. }
  11. -(void) setAge: (NSInteger) age
  12. {
  13. studentAge = age;
  14. }
  15. -(NSString*) studentName
  16. {
  17. return studentName;
  18. }
  19. -(NSInteger) age
  20. {
  21. return studentAge;
  22. }

  23. @end



 

1.5、创建类对象,调用方法。



[cpp]  ​​view plain​​​ ​​​copy​



  1. Student *student = [[Student alloc]init];
  2. [student setStudentName:@"张三"];
  3. [student setAge:10];
  4. [student printInfo];
  5. [student release];



 

 

  • Sutdent *student = [[Sutdent alloc] init]; 这行代码含有几个重要含义
  • [Student alloc]调用Student的类方法,这类似于分配内存,
  • [object init]是构成函数调用,初始类对象的成员变量。

 

打印结果:

 



[plain]  ​​view plain​​​ ​​​copy​



  1. 姓名:张三 年龄:10岁



 

2、类的实例方法使用多个参数

2.1添加一个多参数的方法和实现

 



[cpp]  ​​view plain​​​ ​​​copy​



  1. ....
  2. -(void) setNameAndAge:(NSString*) name setAge:(NSInteger) age;
  3. ....



 



[cpp]  ​​view plain​​​ ​​​copy​



  1. ....
  2. -(void) setNameAndAge:(NSString*) name setAge:(NSInteger) age
  3. {
  4. studentName = name;
  5. studentAge = age;
  6. }
  7. ....



 

2.2调用

 



[cpp]  ​​view plain​​​ ​​​copy​



  1. Student *student = [[Student alloc]init];
  2. [student setNameAndAge:@"李四" setAge:20];
  3. [student printInfo];
  4. [student release];



打印结果:

 

 



[plain]  ​​view plain​​​ ​​​copy​



  1. 姓名:李四 年龄:20岁



 

3、自定义构造函数

3.1声明和实现构造函数

 



[cpp]  ​​view plain​​​ ​​​copy​



  1. ....
  2. -(Student*) initWithNameAndAge:(NSString*) name setAge:(NSInteger) age;
  3. ....



[cpp]  ​​view plain​​​ ​​​copy​



  1. ....
  2. -(Student*) initWithNameAndAge:(NSString*) name setAge:(NSInteger) age
  3. {
  4. self = [super init];

  5. if ( self ) {
  6. [self setNameAndAge:name setAge:age];
  7. }

  8. return self;
  9. }
  10. ....



 

-(id)init 这个方法用于类的初始化创建,每一个类在创建的时候需要调用init方法,使用父类的init 方法得到了self,这就可以做一些子类初始化的工作

3.2使用自定义构造函数:



[cpp]  ​​view plain​​​ ​​​copy​



  1. Student *student = [[Student alloc]initWithNameAndAge:@"rongfzh" setAge:6];
  2. [student printInfo];
  3. [student release];
  4. [student release];