在ios里面,创建一个类,分别会创建一个.h与.m的文件。
有过c++经验的同学,就会发现,.h就是头文件,声明一些函数与变量。.m则是实现文件。
在方法面前需要加一个-\+号,-号代表动态方法,+号代表静态方法
下面是一个student类
Student.h头文件
#import <Foundation/Foundation.h>
//@interface代表声明一个类,NSObject相当于java的Object超类
@interface Student : NSObject
{
//在ios里面定义成员变量习惯在前面加下划线,原因是因为.语法的存在。
int _age;
int _num;
}
//getAge方法
-(int)age;
//setAge方法
-(void )setAge:(int) age;
-(void) setNum:(int) age1 :(int )num2;
//函数末要加@end
@end
Student.m文件
#import "Student.h"
@implementation Student
- (int)age{
return age;
}
-(void)setAge:(int)newAge{
age = newAge;
}
-(void)setNum:(int)age1 :(int)num2{
}
@end
这样就实现了一个类