Objective-C语法之类和对象

1、类和方法

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

 

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

 

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

对应的代码:

 

[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.   
  3. @interface Student : NSObject  
  4.   
  5. @end  
[cpp]  view plain copy
  1. #import "Student.h"  
  2.   
  3. @implementation Student  
  4.   
  5. @end  

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

@interface

 

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Student : NSObject  
  4. {  
  5.     NSString *studentName;  
  6.     NSInteger age;  
  7. }  
  8.   
  9. -(void) printInfo;  
  10. -(void) setStudentName: (NSString*) name;  
  11. -(void) setAge: (NSInteger) age;  
  12. -(NSString*) studentName;  
  13. -(NSInteger) age;  
  14. @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.   
  3. @implementation Student  
  4.   
  5. -(void) printInfo  
  6. {  
  7.     NSLog(@"姓名:%@ 年龄:%d岁",studentName,studentAge);  
  8. }  
  9. -(void) setStudentName: (NSString*) name  
  10. {  
  11.     studentName = name;  
  12. }  
  13. -(void) setAge: (NSInteger) age  
  14. {  
  15.     studentAge = age;  
  16. }  
  17. -(NSString*) studentName  
  18. {  
  19.     return studentName;  
  20. }  
  21. -(NSInteger) age  
  22. {  
  23.     return studentAge;  
  24. }  
  25.   
  26. @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.       
  6.     if ( self ) {  
  7.         [self setNameAndAge:name setAge:age];  
  8.     }  
  9.       
  10.     return self;  
  11. }  
  12. ....  

 

-(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];  

 

 

著作权声明:本文由http://blog.csdn.net/totogo2010/原创

猜你喜欢

转载自duchengjiu.iteye.com/blog/1853283