-----Java培训、Android培训、iOS培训、.Net培训、期待与您交流!-----
一、点语法
1>OC中的点语法是替换set方法和get方法。
Student *stu=[Student new];
[stu setAge:100];
int age=[stu age];
//点语法替换后上面代码后的内容为:
Student *stu=[Student new];
stu.age=100;
int age=stu.age;
int age=[stu age];
2>点语法的本质
点语法的本质是方法的调用,只不过是在编译过程中编译器会自动转换为set方法和get方法。
3>注意点
- (void) setAge:(int)age
{
self.age=age;//产生死循环
}
-(int)age
{
return self.age;//产生死循环
}
二、成员变量的作用域
1>基本概念
局部变量、全局变量都有自己的作用域,成员变量也不例外。
2>类型
@private:只能在当前类的实现@implementation中直接访问。
@protected:可以在当前类以及子类的实现@implementation中直接访问。
@public:任何地方都可以访问。
@package:同一个"框架"内可以访问,介于@private和@public之间。
3>注意点
没有@interface,只有@implementation,也可以开发一个类,但在其中的成员变量作用域是@private。
4>实例代码
#import <Foundation/Foundation.h>
//-------------person声明---------//
@interface person:NSObject
{
@public
int _age;
@private
int _height;
@protected
int _weight;
}
- (void) setAge:(int)age;
- (int)age;
- (void)setHeight:(int)height;
- (void)height;
- (void)setWeight:(int)weight;
- (int)weight;
@end
//---------person实现-----------//
@implementation person
//年龄setter
- (void) setAge:(int)age
{
_age=age;
}
//年龄getter
- (int)age
{
return _age;
}
//身高setter
- (void)setHeight:(int)height
{
_height=height;
}
//身高getter
- (void)height
{
return _height;
}
//体重getter
- (void)setWeight:(int)weight
{
_weight=weight;
}
//体重setter
- (int)weight
{
return _weight;
}
@end
//-------------student声明---------//
@interface student:person
{
}
- (void) run;
@end
//---------student实现-----------//
@implementation student
- (void)run
{
NSLog(@"年龄%i岁的学生在跑步",_age);//可以访问_age变量
//NSLog(@"身高为%d的学生在跑步",_height);//不能访问_height变量
NSLog(@"体重为%d的学生在跑步",_weight);//可以访问_weight变量
}
@end
int main()
{
student *s=[stuent new];
[s run];
}
三、@property和@synthesize
1>@property
用在@interface中,用来自动生成setter和getter的声明
用@property int age; 就能替换为下面两行代码:
- (void)setAge:(int)age;
- (int)age;
2>@synthesize
用在@implementation中,用来自动生成setter和getter的实现。
手动实现:
1.若手动实现setter,编译器就只会生成getter;
2.若手动实现getter,编译器就只会生成setter;
3.若同时生getter和setter编译器就不会自动生成不存在的变量;
用@synthesize age;//(@synthesize age=age 或 @synthesize age=_age) 就能替换为下面的代码:
- (void)setAge:(int)age
{
_age=age;
}
- (int)age
{
return _age;
}
3>在xcode 4.x以后就不用再写@synthesize。
只需要在@interface中写:@property int age;就能生成成员变量、getter和setter方法的声明、getter和setter方法的实现,(缺点:作用域是private); 生成的变量名是下划线开头{_age}。
四、id万能指针
1>作用:能指向、操作任何oc对象。
2>示例代码
#import <Foundation/Foundation.h>
@interface person:NSObject
//age属性
@property int age;
@end
@implementation person
@end
//程序入口
int main
{
//person对象
person *p=[person new];
[p setAge:10];
NSLog(@"%d",[p age]);
//id指针
id pp=[person new];
[pp setAge:100];
NSLog(@"%d",[pp age]);
}
五、构造方法
在初始化对象的时候操作对象成员、赋值等操作。
1>重写构造方法
#import <Foundation/Foundation.h>
@interface person:NSObject
{
int _age;
}
@end
@implementation person
- (id)init
{
if(self==[super init])
{
//成员变量赋值等操作
_age=10;
NSLog(@"重写init赋值age成功");
}
}
@end
int main()
{
/* 完整的创建一个对象需要2个步骤
1.分配存储空间 +alloc
2.初始化 -init
*/
person *p=[person alloc];
[p init];
}
2>自定义构造方法
#import <Foundation/Foundation.h>
@interface person:NSObject
{
int _age;
}
@end
@implementation person
- (id)initAge:age
{
if(self==[super init])
{
//成员变量赋值等操作
_age=age;
NSLog(@"initAge赋值age成功");
}
}
@end
int main()
{
/* 完整的创建一个对象需要2个步骤
1.分配存储空间 +alloc
2.初始化 -init
*/
person *p=[[person alloc] initAge:100];
NSLog(@"%d",p->_age);
return 0;
}