1.OC类
.h文件 类、函数的生命 @interface @end
.m文件 类的具体实现 @implementation @end
2.创建OC对象
Dog *dog=[Dog alloc]; alloc=new
初始化构造函数
[dog init];
销毁
[dog release];
3.类中字段和函数
@interface Dog:NSObject{
//定义字段
int age;
}
-(void) setAge:(int)newAge;
//定义函数
@end
4.字段定义
@public @protected @private 缺省@protected
Dog.h
@interface Dog:NSObject
{
@public
int age;
@protected
int ID;
@private
float price;
}
@end
5.类得声明
Dog * myDog;
*表示指针,也表示引用
可以通过myDog-->dog 或者myDog.dog这些方法来访问
6.典型(example)
Dog.h
#import <Foundation/Foundation.h>
@interface Dog:NSObject{
int age;
}
-(id) init;
-(id) initWithAge:(int)newAge;
-(int)getAge;
-(void)setAge:(int)newAge;
@end
------------------------------------------------
Dog.m
#import "Dog.h"
@implementation Dog
-(id)init{
return [self initWithAge:10];
}
-(id)initWithAge:(int)newAge{
self=[super init];
if(self){
age=newAge;
}
return self;
}
....
6.函数定义
O-C属性声明 只能在@interface{和}之间
方法定义
-(int)f:(int)x;
这里的-表示对象的方法,+表示类得方法
-(int) insertObject:(NSObject *)o AtIndex:(int)index
Before:(BOOL)before
int ret=[obj insertObject:10 AtIndex:2 Before:TRUE]
7.函数重载
OC不是严格的函数重载
@interface Foo:NSObject{
}
-(int) g:(int)x;
-(int) g:(float)x; //错误 方法冲突 (无标签)
-(int) g:(int)x:(int)y;
-(int) g:(int)x:(float)y; //错误
函数类型 不管 --只管函数名