voi------<a href="http://write.blog.csdn.net/postedit" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
第六讲--oc的基本语法
01-oc的声明与方法
oc的声明也就是接口,简称API,放在.h文件中
@interface circle:NSobject
{
ShapeColor _fillColor;
ShapeRect _bound;
}
-(void) setFillColor:(ShapeColor)c
-(void)setBound:(ShapeRect)b
+(void) age;
@end
// 接口继承于父类NSobject
//两个成员变量 _fillColor,_bound;
//声明了 setFillColor成员变量方法,无返回值
//声明了setBound成员变量方法,无返回值
//这个age,是类方法,类方法必须类调用
//成员方法,可以用类实例化的成员变量调用
@implementation Circle
-(void) setFillColor:(ShapeColor)c
{
_fillColor=c;
}
-(void)setBound:(ShapeRect)b
{
_bound=b;
}
@end
//这个就是方法的实现
//将值传给成员变量
//方法都用-开头
02-类的调用,set,get方法
1.先建个studen类
@interface studen :NSobject
{
NSstring _name;
int _age;
}
-(void) Set_name:(int) name;
-(NSstring)name;
-(void)Set_age:(int) age;
-(int)age
@end
@implementation student
-(void) Set_name:(int) name;
{
_name=name; //给成员变量赋值
}
-(NSstring)name;
{
return _name; //返回成员变量
}
-(void)Set_age:(int) age;
{
_age=age;
}
-(int)age
{
return _age;
}
int main()
{
studet * stu=[[student allow]init];
//初始化一个student的对象
//student *stu=[student new];两者相当
[stu Set_age:18];
[stu Set_name:zhangsan];
//传值进去
NSlog(@name=%@,age=%d,age,name);
//NSlog 是输出
//%@是oc对象的输出符合。
}