点语法
// #import @interface Girl : NSObject { } - (void)setAge:(int)age; - (int)age; - (void)setName:(NSString *)name; - (NSString *)name; - (void)setTel:(NSString *)tel; - (NSString *)tel; - (void)test; @end // #import "Girl.h" @implementation Girl - (void)setAge:(int)age { } - (int)age { } - (void)setName:(NSString *)name { } - (NSString *)name { } - (void)setTel:(NSString *)tel { } - (NSString *)tel { } - (void)test { } - (NSString *)description { } @end // #import #import "Girl.h" int main(int argc, const char * argv[]) { // // // // // // // // // // // // // // // // // // // // // // } |
成员变量作用域—访问控制符
// #import "Person.h" @interface Girl : Person - (void)test; @end // #import "Girl.h" @implementation Girl - (void)test { // // // // } @end // #import @interface Person : NSObject { // } - (void)test; @end // #import "Person.h" @implementation Person - (void)test { // // // } @end // #import #import "Person.h" #import "Girl.h" int main(int argc, const char * argv[]) { // // // // // // // // // // } |
类的本质
// #import @interface Person : NSObject { } - (void)test; @end // #import "Person.h" @implementation Person - (void)test { // // } @end // #import "Person.h" @interface Student : Person @end // #import "Student.h" @implementation Student @end // #import @interface Dog : NSObject @end // #import "Dog.h" @implementation Dog @end // #import #import "Person.h" #import "Dog.h" typedef struct { }Date; int main(int argc, const char * argv[]) { // // // // } |
property
// #import @interface Person : NSObject { } @property NSString * name; @property int no; //property是一个编译器特性 @property int age;//注意:这个地方成员变量的名称写去掉下划线后的 //- (void)setAge:(int)age; //- (int)age; //- (void)setNo:(int)no; //- (int)no; @end // #import "Person.h" @implementation Person - (void)setAge:(int)age { } - (int)age { } - (void)setNo:(int)no { } - (int)no { } - (void)setName:(NSString *)name { } - (NSString *)name { } //- (void)set_age:(int)_age //{ // //} // //- (int)_age //{ // //} @end // #import #import "Person.h" int main(int argc, const char * argv[]) { } |
synthesize
// #import @interface Person : NSObject { } //@property NSString * name; //@property int no; @property int age; @end // #import "Person.h" @implementation Person //@synthesize 用来自动生成get/set方法的实现 // @synthesize age 意思是给.h文件中名称叫做age的property生成实现 //@synthesize age = _age; //@synthesize age = _no; // 如果没有明确告诉synthesize后面的age要赋值给谁它就会赋值给和它同名的成员变量 // 但是不明确使用synthsize时(即只有property)age赋值给_age @synthesize age; //- (void)setAge:(int)age //{ // //} //- (int)age //{ // //} @end // #import #import "Person.h" int main(int argc, const char * argv[]) { // // // // } |
property增强(自动生成私有实现)
// #import @interface Person : NSObject { // // // // } @property int age; - (void)personInfomation; @end // #import "Person.h" @implementation Person //{ // //} - (void)personInfomation { // } @end // #import #import "Person.h" int main(int argc, const char * argv[]) { // // // 上面两句验证:如果没有手动声明成员变量,perperty会在.m文件中自动帮我们生成一个_开头的成员变量 } |
重写set/get
// #import @interface Person : NSObject @property int age; @property NSString * name; @end // #import "Person.h" @implementation Person // 重写set方法 // 1.当想控制外界传入的一些数据的合理性安全性的时候我们就需要手动实现set方法 - (void)setAge:(int)age { } // 2.当我们手动同时实现了set/get方法的时候,property就不会自动给我们生成成员变量 //- (int)age //{ // //} @end // #import #import "Person.h" int main(int argc, const char * argv[]) { // } |
OC私有方法
// #import @interface Person : NSObject //- (void)test; @end // #import "Person.h" @implementation Person // 只有实现,没有声明我们称之为私有方法 - (void)test { } @end // #import #import "Person.h" int main(int argc, const char * argv[]) { } |
id类型
• • 数据类型的共同特性 Char float doubl int BOOL NSObject * 1.都能够声明称变量 2.都能够做位方法的参数传递,也作为方法得 返回值 3.都能够声明称类的成员变量 Id var = ? - - - // #import @interface Person : NSObject @property int age; @end // #import "Person.h" @implementation Person @end // #import #import "Person.h" int main(int argc, const char * argv[]) { // // // // // // // // // } |
构造方法基本概念
// #import @interface Person : NSObject @property int age; @end // #import "Person.h" @implementation Person // 优化init方法 - (id)init { } @end // #import #import "Person.h" int main(int argc, const char * argv[]) { // // // // // // // } |
构造方法应用场景
// #import @interface Gun : NSObject //{ // //} @property int bulletCount; // 射击方法 - (void)shoot; @end // #import "Gun.h" @implementation Gun - (id)init { } - (void)shoot { } @end // #import #import "Gun.h" @interface Police : NSObject //{ // //} @property Gun * gun; // 开枪 - (void)fire; @end // #import "Police.h" #import "Gun.h" @implementation Police - (id)init { // // // } - (void)fire { // } @end // #import #import "Police.h" int main(int argc, const char * argv[]) { // // // // // // } |
自定义构造方法
// #import typedef enum { }Gender; @interface Person : NSObject @property int age; @property Gender gender; - (id)initWithAge:(int)age; - (id)initWithAge:(int)age andGender:(Gender)gender; - (void)infomation; @end // #import "Person.h" @implementation Person - (id)init { } - (id)initWithAge:(int)age { // } - (id)initWithAge:(int)age andGender:(Gender)gender { } - (void)infomation { } @end // #import #import "Person.h" int main(int argc, const char * argv[]) { // // } |
继承中自定义构造方法
// #import @interface Person : NSObject //{ // //} @property int age; - (id)initWithAge:(int)age; - (void)infomation; @end // #import "Person.h" @implementation Person - (id)init { } - (id)initWithAge:(int)age { } - (void)infomation { } @end // #import "Person.h" @interface Student : Person @property NSString * name; - (id)initWithAge:(int)age andName:(NSString *)name; @end // #import "Student.h" @implementation Student - (id)initWithAge:(int)age andName:(NSString *)name { } - (void)infomation { } @end // #import #import "Person.h" #import "Student.h" int main(int argc, const char * argv[]) { // // // // // // // } |
类方法创建对象(难点)
//主要演示 如何 定义类方法创建Person对象(要让它的子类也能用) 及注意点 // #import @interface Person : NSObject @property int age; // 定义类方法创建Person对象 // 一般开发中会提供一个对象方法和一个类方法用于创建初始化对象 // 约定:只要是用于创建对象的类方法,方法名称和类名一致(首字母小写) + (id)person; + (id)personWithAge:(int)age; @end // #import "Person.h" @implementation Person + (id)person { // // // // // } + (id)personWithAge:(int)age { } @end // #import "Person.h" @interface Student : Person @property NSString * name; @end // #import "Student.h" @implementation Student @end // #import #import "Person.h" #import "Student.h" int main(int argc, const char * argv[]) { // // } |
类的互引用(需要再理解)
// #import //#import "Person.h" 如果在A类中导入了B类,在B类中又导入了A类,就会造成循环引用 @class Person; //仅仅用于告诉编译Person是一个类并没有导入person的头文件,也就是说在本文件中用到person类中的 @interface Car : NSObject - (void)run:(Person *)person @end // #import "Car.h" //在.m中使用到改类的时候必须导入改类的头问题(声明) #import "Person.h" @implementation Car - (void)run:(Person *)person { } @end // #import #import "Car.h" @interface Person : NSObject { } @property NSString * name; - (void)run; @end // #import "Person.h" @implementation Person - (void)run { } - (NSString *)description { } @end // #import #import "Person.h" #import "Car.h" int main(int argc, const char * argv[]) { } |