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

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

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

1.2、生成student.h 和student.m
- #import
<Foundation/Foundation.h> -
- @interface
Student : NSObject -
- @end
- #import
"Student.h" -
- @implementation
Student -
- @end
1.3、在头文件里添加类成员变量和方法
@interface
- #import
<Foundation/Foundation.h> -
- @interface
Student : NSObject - {
-
NSString *studentName; -
NSInteger age; - }
-
- -(void)
printInfo; - -(void)
setStudentName: (NSString*) name; - -(void)
setAge: (NSInteger) age; - -(NSString*)
studentName; - -(NSInteger)
age; - @end
- @interface 类的开始的标识符号 ,好比Java
或 C 语言中的Class - @end 类的结束符号
- 继承类的方式:Class:Parent,如上代码Student:NSObject
- 成员变量在@interface Class: Parent { .... }之间
- 成员变量默认的访问权限是protected。
- 类成员方法在成员变量后面,格式是:: scope (returnType) methodName: (parameter1Type) parameter1Name;
- scope指得是类方法或实例化方法。类方法用+号开始,实例化方法用 -号开始。
1.4、实现类中的方法
@implementation
- #import
"Student.h" -
- @implementation
Student -
- -(void)
printInfo - {
-
NSLog(@"姓名:%@ 年龄:%d岁",studentName,studentAge); - }
- -(void)
setStudentName: (NSString*) name - {
-
studentName = name; - }
- -(void)
setAge: (NSInteger) age - {
-
studentAge = age; - }
- -(NSString*)
studentName - {
-
return studentName; - }
- -(NSInteger)
age - {
-
return studentAge; - }
-
- @end
1.5、在View中创建类对象,调用方法。
- -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; -
-
Student *student = [[Student alloc]init]; -
[student setStudentName:@"张三"]; -
[student setAge:10]; -
[student printInfo]; -
[pool release]; -
- }
- Sutdent *student = [[Sutdent alloc] init]; 这行代码含有几个重要含义
-
[Student alloc]调用Student的类方法,这类似于分配内存, -
[object init]是构成函数调用,初始类对象的成员变量。
- 姓名:张三
年龄:10岁
2、类的实例方法使用多个参数
2.1添加一个多参数的方法和实现
- ....
- -(void)
setNameAndAge:(NSString*) name setAge:(NSInteger) age; - ....
- ....
- -(void)
setNameAndAge:(NSString*) name setAge:(NSInteger) age - {
-
studentName = name; -
studentAge = age; - }
- ....
2.2调用
- -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; -
-
Student *student = [[Student alloc]init]; -
[student setNameAndAge:@"李四" setAge:20]; -
[student printInfo]; -
[student release]; -
[pool release]; - }
- 姓名:李四
年龄:20岁
3、自定义构造函数
3.1声明和实现构造函数
- ....
- -(Student*)
initWithNameAndAge:(NSString*) name setAge:(NSInteger) age; - ....
- ....
- -(Student*)
initWithNameAndAge:(NSString*) name setAge:(NSInteger) age - {
-
self = [super init]; -
-
if ( self ) { -
[self setNameAndAge:name setAge:age]; -
} -
-
return self; - }
- ....
-(id)init 这个方法用于类的初始化创建,每一个类在创建的时候需要调用init方法,使用父类的init 方法得到了self,这就可以做一些子类初始化的工作
3.2使用自定义构造函数:
- -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; -
Student *student = [[Student alloc]initWithNameAndAge:@"rongfzh" setAge:6]; -
[student printInfo]; -
[student release]; -
[pool release]; - }