类:具有相同功能的对象的集合,是抽象的
对象:通过类创建的一个真实的实例,是具体的
简单的来说,比如学生是一个类,因为学生里面包含了学生1,学生2等等,而学生1,学生2这些就是具体的对象。
在oc中,我们创建的类会生成两个文件:.h头文件 和 .m源文件,.h头文件中存放的是类的声明,.m中存放的是类的实现。
接下来,用代码举例:
@interface Student : NSObject{
//声明实例变量
NSString *_name;
NSUInteger _age;
float _height;
}
//添加访问器的方法
-(void)setName:(NSString *)name;
-(NSString *)name;
-(void)setAge:(NSUInteger)age;
-(NSUInteger)age;
-(void)setHeight:(float)height;
-(float)height;
//对象方法(多参数)
//方法名是setName:和andAge:组成的。
-(void)setName:(NSString *)name andAge:(NSUInteger)age;
-(void)setName:(NSString *)name andAge:(NSUInteger)age andHeight:(float)height;
//声明一个没有返回值没有参数的对象(实例)方法,用来展示当前学生的信息
-(void)showStuInfo;
//类方法(以+开头)
+(Student *)student;
//多参数的类方法(类方法以+开头,最好以类名作为方法名的开头)(Student *)也可以写成(instancetype)或者(id)
+(Student *)studentWithName:(NSString *)name andAge:(NSUInteger)age andHeight:(float)height;
//对象方法名和类的方法名可以一样
-(void)test;
+(void)test;
@implementation Student
//设置器的方法:为实例变量赋值
-(void)setName:(NSString *)name{
//实例变量 = 传入的参数值
_name = name;
}
//获取器的方法:获取实例变量的值
-(NSString *)name{
//将实例变量的值返回
return _name;
}
-(void)setAge:(NSUInteger)age{
_age = age;
}
-(NSUInteger)age{
return _age;
}
-(void)setHeight:(float)height{
_height = height;
}
-(float)height{
return _height;
}
//展示学生信息的对象方法
-(void)showStuInfo{
// NSLog(@"学生的姓名:%@ 年龄:%lu 身高:%@",_name,_age,_height?[NSString stringWithFormat:@"%.1fcm",_height ]:@"未知");
// 打印self对象,就相当于[self description]
NSLog(@"%@", self);
}
//多参数的对象方法
-(void)setName:(NSString *)name andAge:(NSUInteger)age{
// 1.直接对实例变量赋值
// _name = name;
// _age = age;
// 2.通过点语法调用访问器的方法
// self.name = name;
// self.age = age;
// 3.直接调用访问器的方法
[self setName:name];
[self setAge:age];
}
-(void)setName:(NSString *)name andAge:(NSUInteger)age andHeight:(float)height{
// 1.
// _name = name;
// _age = age;
// _height = height;
// 2.在对象方法中,可以通过self调用其他的对象方法
// 在对象方法中,self表示的就是对象本身
[self setName:name andAge:age];
_height = height;
}
//类方法(以+开头),相当于类方法 new
+(Student *)student{
Student *stu = [[Student alloc] init];
return stu;
}
//多参数的类方法
+(Student *)studentWithName:(NSString *)name andAge:(NSUInteger)age andHeight:(float)height{
//类方法中不能直接使用实例变量,因为实例变量属于对象,即不能用_name = name;
// 1.alloc+init
// Student *stu = [[Student alloc] init];
// 2.new
// Student *stu = [Student new];
// 3.自定义的类方法,用类名调用
// Student *stu = [Student student];
// 4.自定义的类方法,在类方法中self表示类
Student *stu = [self student];
// stu.name = name;
// stu.age = age;
// 在类方法中,self表示的是类
[stu setName:name andAge:age andHeight:height];
//调用测试方法
[self test];
return stu;
}
//对象方法和类方法可以同名
-(void)test{
NSLog(@"对象方法 test");
}
+(void)test{
NSLog(@"类方法 test");
}
//实现NSObject类中声明的描述对象信息的方法
-(NSString *)description{
return [NSString stringWithFormat:@"学生姓名:%@ 年龄:%lu 身高:%.1f",_name,_age,_height];
}
@end
在main.m中创建对象
#import
#import "Student.h" //要先导入创建的类的头文件
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建学生对象,写法一
//alloc 申请内存 init 初始化
Student *stu1 = [[Student alloc] init];
//写法二
// Student *stu1 = [Student alloc];
// stu1 = [stu1 init];
//写法三
// Student *stu1 = [Student new];
//利用点语法进行赋值,也可以通过访问器的方法进行赋值
stu1.name = @"mary"; //可以写成[stu1 setName:"mary"];
stu1.age = 20; //可以写成 [stu1 setAge:20];
//通过[对象名 方法名]调用方法
[stu1 showStuInfo];
Student *stu2 = [[Student alloc] init];
//通过多参数的对象方法赋值
[stu2 setName:@"happy" andAge:12];
[stu2 showStuInfo];
Student *stu3 = [[Student alloc] init];
[stu3 setName:@"gray" andAge:21 andHeight:186];
[stu3 showStuInfo];
// 通过类方法,得到学生对象
Student *stu4 = [Student student];
// 相当于Student *stu4 = [Student new];
[stu4 setName:@"nazi" andAge:21 andHeight:185];
[stu4 showStuInfo];
// 通过多参数的类方法,快速得到有初值的对象
Student *stu5 = [Student studentWithName:@"lucy" andAge:20 andHeight:168];
[stu5 showStuInfo];
}
return 0;
}