【IOS开发笔记01】学生管理系统(上)

时间:2021-01-01 04:15:02

端到端的机会

虽然现在身处大公司,但是因为是内部创业团队,产品、native、前端、服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一大坨出去了,这个时候立刻想到了切入IOS开发!!!

事实上,前端开发做到一定时间,要进步很难了,最近几个月扑到业务上便感觉突破不了目前的瓶颈,自身的前端瓶颈主要在两方面:技术深度、技术广度

其实不论深度或者广度来说都不是简单前端能说清楚的事情,不能说了解了angularJS、react等框架技术深度就深了,因为事实上angular中包含了很多设计思想,学习他是编程思想的提升,并不单是js功力的提升。

要说自身职业规划,前端当然可以往nodeJS发展走大前端方向,但是这个真的需要项目支持,多次尝试自学nodeJS皆收效甚微,便是因为没有实际项目支持,说白了没有人带着做项目。

而团队内部有人带着做IOS开发,我居然可以从零开始自学然后参与生产项目开发,想想真的令人兴奋!!!

但是天下没有不要钱的午餐,切入IOS开发的前提是保证现有H5任务的工期以及质量,所以加班什么的在所难免,可是我怎么可能放弃这种千载难逢的好事呢?于是立马给技术老大说了愿意多承担工作的意愿,老大也很nice的答应我可以切入IOS开发,于是这一切便如此美好的开始了!所以接下来一段时间只需要fighting就够了!!!

如何学习新语言

今时不同往日,已经不可能有太多的空闲时间给我学习了,刚开始也想过应该系统的学习,一章一章的巩固知识,但是这样效率太低,等我学完都猴年马月了,项目早结束了

所以现在适合我的学习方法是做简单并且熟悉多项目,比如大一的C语言考试,学生管理系统

需求说明

简单设计一个学生管理系统,要求具有以下功能:

1 可以录入学生姓名,性别、课程等信息

2 可以给各门课程录入考试成绩

3 支持姓名排序,班级排序,成绩排序

因为最初做项目是为了熟悉语言,所以不需要太复杂,于是我们便开始吧!!!

学生类的设计

你要开发IOS程序,首先得有一台Mac机,其次需要安装xcode开发工具,我反正是去借了一台,然后让同事考了一个最新版的xcode,于是开始开发吧。

OC中的类

OC中的类皆继承至NSObject类,会带有一些特有并且经常会用到的方法,具体细节我们不去纠结,直接创建类吧

新建一个类会形成两个文件:file.h头文件与file.m为类的具体实现文件,我们这里新建一个Student类:

 #import <Foundation/Foundation.h>

 @interface Student : NSObject

 @end
#import "Student.h"

@implementation Student

@end

我们这里不去吐槽OC的怪异语法,因为我们如果得去学习一个东西,就不要吐槽他,这样会分散你的注意力并且会让学习难以继续,所以回到正题

属性

OC的属性定义在头文件中,以学生来说我们规定其有以下属性,其中课程真实场景会被抽象为一个类,所以我们也这样做吧,新建Course类,并且给学生的属性如下:

 #import <Foundation/Foundation.h>
#import "Course.h" @interface Student : NSObject
{
NSString *_name;
int _age;
NSString *_sex;
Course *_chinese;
Course *_math;
//录入时间
NSDate *_dateCreate;
}
@end

课程类只具有名字与得分两个属性:

 #import <Foundation/Foundation.h>

 @interface Course : NSObject
{
NSString *_name;
float _score;
}
@end

其中下划线定写法为OC的规则,我们不需要知道他为什么要这样做,先做再说,后面熟悉了自然就知道了,与C#一样,属性会有getter与setter方法,OC这里提供了语法糖,我们暂不使用,老老实实的写代码,下面为两个类的具体实现:

 #import <Foundation/Foundation.h>

 @interface Course : NSObject
{
NSString *_name;
float _score;
} -(void)setName: (NSString *)str;
-(NSString *)name; -(void)setScore: (float)fl;
-(float)score; @end #import "Course.h" @implementation Course -(void) setName:(NSString *)str
{
_name = str;
} -(NSString *) name
{
return _name;
} -(void) setScore:(float)fl
{
_score = fl;
} -(float) score
{
return _score;
} @end #import <Foundation/Foundation.h>
#import "Course.h" @interface Student : NSObject
{
NSString *_name;
int _age;
NSString *_sex;
Course *_chinese;
Course *_math;
//录入时间
NSDate *_dateCreate;
} -(void)setName: (NSString *)str;
-(NSString *)name; -(void)setAge: (int)a;
-(int)age; -(void)setSex: (NSString *)str;
-(NSString *)sex; -(void)setChinese: (Course *)c;
-(Course *)chinese; -(void)setMath: (Course *)c;
-(Course *)math; //只暴露读取接口
-(NSDate *)dateCreate; @end #import "Student.h" @implementation Student -(void) setName:(NSString *)str
{
_name = str;
} -(NSString *) name
{
return _name;
} -(void)setAge: (int)a
{
_age = a;
} -(int)age
{
return _age;
} -(void)setSex: (NSString *)str
{
_sex = str;
} -(NSString *)sex
{
return _sex;
} -(void)setChinese: (Course *)c
{
_chinese = c;
} -(Course *)chinese
{
return _chinese;
} -(void)setMath: (Course *)c
{
_math = c;
} -(Course *)math
{
return _math;
} //只暴露读取接口
-(NSDate *)dateCreate
{
return _dateCreate;
} @end

Student-Course

构造函数

构造函数是每个类实例化的入口点,每一个继承至NSObject的对象都会有一个init的实例方法,这个便是其构造函数,我们这里自定义构造函数,Course与Student的构造函数

 #import <Foundation/Foundation.h>

 @interface Course : NSObject
{
NSString *_name;
float _score;
} -(instancetype)initWithName:(NSString *)newName andScore:(float)newScore; -(void)setName: (NSString *)str;
-(NSString *)name; -(void)setScore: (float)fl;
-(float)score; @end #import "Course.h" @implementation Course //自定义构造方法
-(instancetype)initWithName:(NSString *)newName andScore:(float)newScore
{
self = [super init];
if (self) {
_name = newName;
_score = newScore;
}
return self;
} -(void) setName:(NSString *)str
{
_name = str;
} -(NSString *) name
{
return _name;
} -(void) setScore:(float)fl
{
_score = fl;
} -(float) score
{
return _score;
} @end
 #import <Foundation/Foundation.h>
#import "Student.h"
#import "Course.h" int main(int argc, const char * argv[]) {
@autoreleasepool { //alloc方法创建实例空间,init初始化
Course *c = [[Course alloc] initWithName:@"叶小钗" andScore:]; NSLog(@"%@, %f", c.name, c.score); }
return ;
}

成功打印出我们想要的代码,所以这个时候再将Student类的构造方法加上,并且给Student释放一个对外实例方法:showData

 #import <Foundation/Foundation.h>

 @interface Course : NSObject
{
NSString *_name;
float _score;
} -(instancetype)initWithName:(NSString *)newName andScore:(float)newScore; -(void)setName: (NSString *)str;
-(NSString *)name; -(void)setScore: (float)fl;
-(float)score; -(void)showData; @end #import "Course.h" @implementation Course //自定义构造方法
-(instancetype)initWithName:(NSString *)newName andScore:(float)newScore
{
self = [super init];
if (self) {
_name = newName;
_score = newScore;
}
return self;
} -(void) setName:(NSString *)str
{
_name = str;
} -(NSString *) name
{
return _name;
} -(void) setScore:(float)fl
{
_score = fl;
} -(float) score
{
return _score;
} -(void) showData
{
NSLog(@"课程名:%@", _name);
NSLog(@"课程得分:%f",_score);
} @end #import <Foundation/Foundation.h>
#import "Course.h" @interface Student : NSObject
{
NSString *_name;
int _age;
NSString *_sex;
Course *_chinese;
Course *_math;
//录入时间
NSDate *_dateCreate;
} -(instancetype)initWithName:(NSString *)newName andAge:(int)newAge andSex:(NSString *)newSex andChinese:(Course *) newChinese andMath:(Course *) newMath; -(void)setName: (NSString *)str;
-(NSString *)name; -(void)setAge: (int)a;
-(int)age; -(void)setSex: (NSString *)str;
-(NSString *)sex; -(void)setChinese: (Course *)c;
-(Course *)chinese; -(void)setMath: (Course *)c;
-(Course *)math; //只暴露读取接口
-(NSDate *)dateCreate; -(void) showData; @end #import "Student.h" @implementation Student -(instancetype)initWithName:(NSString *)newName andAge:(int)newAge andSex:(NSString *)newSex andChinese:(Course *) newChinese andMath:(Course *) newMath
{
self = [super init];
if (self) {
_name = newName;
_age = newAge;
_sex = newSex;
_chinese = newChinese;
_math = newMath;
_dateCreate = [[NSDate alloc] init];
}
return self;
} -(void) setName:(NSString *)str
{
_name = str;
} -(NSString *) name
{
return _name;
} -(void)setAge: (int)a
{
_age = a;
} -(int)age
{
return _age;
} -(void)setSex: (NSString *)str
{
_sex = str;
} -(NSString *)sex
{
return _sex;
} -(void)setChinese: (Course *)c
{
_chinese = c;
} -(Course *)chinese
{
return _chinese;
} -(void)setMath: (Course *)c
{
_math = c;
} -(Course *)math
{
return _math;
} //只暴露读取接口
-(NSDate *)dateCreate
{
return _dateCreate;
} -(void) showData
{
NSLog(@"姓名:%@", _name);
NSLog(@"性别:%@", _sex);
NSLog(@"年龄:%d", _age);
[_chinese showData];
[_math showData]; } @end
 #import <Foundation/Foundation.h>
#import "Student.h"
#import "Course.h" int main(int argc, const char * argv[]) {
@autoreleasepool { //alloc方法创建实例空间,init初始化
Course *chinese = [[Course alloc] initWithName:@"语文" andScore:];
Course *math = [[Course alloc] initWithName:@"数学" andScore:]; Student *s = [[Student alloc] initWithName:@"叶小钗" andAge: andSex:@"男" andChinese:chinese andMath:math]; [s showData]; }
return ;
}
-- ::24.853 student[:] 姓名:叶小钗
-- ::24.854 student[:] 性别:男
-- ::24.854 student[:] 年龄:
-- ::24.855 student[:] 课程名:语文
-- ::24.855 student[:] 课程得分:90.000000
-- ::24.855 student[:] 课程名:数学
-- ::24.855 student[:] 课程得分:95.000000
Program ended with exit code:

如此一来,基本的功能便具备了,今天的内容也暂时到此