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

端到端的机会

虽然现在身处大公司,但是因为是内部创业团队,产品、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类:

1 #import <Foundation/Foundation.h>
2 
3 @interface Student : NSObject
4 
5 @end
#import "Student.h"

@implementation Student

@end

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

属性

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

 1 #import <Foundation/Foundation.h>
 2 #import "Course.h"
 3 
 4 @interface Student : NSObject
 5 {
 6     NSString *_name;
 7     int _age;
 8     NSString *_sex;
 9     Course *_chinese;
10     Course *_math;
11     //录入时间
12     NSDate *_dateCreate;
13 }
14 @end

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

1 #import <Foundation/Foundation.h>
2 
3 @interface Course : NSObject
4 {
5     NSString *_name;
6     float _score;
7 }
8 @end

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

  1 #import <Foundation/Foundation.h>
  2 
  3 @interface Course : NSObject
  4 {
  5     NSString *_name;
  6     float _score;
  7 }
  8 
  9 -(void)setName: (NSString *)str;
 10 -(NSString *)name;
 11 
 12 -(void)setScore: (float)fl;
 13 -(float)score;
 14 
 15 @end
 16 
 17 #import "Course.h"
 18 
 19 @implementation Course
 20 
 21 -(void) setName:(NSString *)str
 22 {
 23     _name = str;
 24 }
 25 
 26 -(NSString *) name
 27 {
 28     return _name;
 29 }
 30 
 31 -(void) setScore:(float)fl
 32 {
 33     _score = fl;
 34 }
 35 
 36 -(float) score
 37 {
 38     return _score;
 39 }
 40 
 41 @end
 42 
 43 #import <Foundation/Foundation.h>
 44 #import "Course.h"
 45 
 46 @interface Student : NSObject
 47 {
 48     NSString *_name;
 49     int _age;
 50     NSString *_sex;
 51     Course *_chinese;
 52     Course *_math;
 53     //录入时间
 54     NSDate *_dateCreate;
 55 }
 56 
 57 -(void)setName: (NSString *)str;
 58 -(NSString *)name;
 59 
 60 -(void)setAge: (int)a;
 61 -(int)age;
 62 
 63 -(void)setSex: (NSString *)str;
 64 -(NSString *)sex;
 65 
 66 -(void)setChinese: (Course *)c;
 67 -(Course *)chinese;
 68 
 69 -(void)setMath: (Course *)c;
 70 -(Course *)math;
 71 
 72 //只暴露读取接口
 73 -(NSDate *)dateCreate;
 74 
 75 @end
 76 
 77 #import "Student.h"
 78 
 79 @implementation Student
 80 
 81 -(void) setName:(NSString *)str
 82 {
 83     _name = str;
 84 }
 85 
 86 -(NSString *) name
 87 {
 88     return _name;
 89 }
 90 
 91 -(void)setAge: (int)a
 92 {
 93     _age = a;
 94 }
 95 
 96 -(int)age
 97 {
 98     return _age;
 99 }
100 
101 -(void)setSex: (NSString *)str
102 {
103     _sex = str;
104 }
105 
106 -(NSString *)sex
107 {
108     return _sex;
109 }
110 
111 -(void)setChinese: (Course *)c
112 {
113     _chinese = c;
114 }
115 
116 -(Course *)chinese
117 {
118     return _chinese;
119 }
120 
121 -(void)setMath: (Course *)c
122 {
123     _math = c;
124 }
125 
126 -(Course *)math
127 {
128     return _math;
129 }
130 
131 //只暴露读取接口
132 -(NSDate *)dateCreate
133 {
134     return _dateCreate;
135 }
136 
137 @end
Student-Course

构造函数

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

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Course : NSObject
 4 {
 5     NSString *_name;
 6     float _score;
 7 }
 8 
 9 -(instancetype)initWithName:(NSString *)newName andScore:(float)newScore;
10 
11 -(void)setName: (NSString *)str;
12 -(NSString *)name;
13 
14 -(void)setScore: (float)fl;
15 -(float)score;
16 
17 @end
18 
19 #import "Course.h"
20 
21 @implementation Course
22 
23 //自定义构造方法
24 -(instancetype)initWithName:(NSString *)newName andScore:(float)newScore
25 {
26     self = [super init];
27     if (self) {
28         _name = newName;
29         _score = newScore;
30     }
31     return self;
32 }
33 
34 -(void) setName:(NSString *)str
35 {
36     _name = str;
37 }
38 
39 -(NSString *) name
40 {
41     return _name;
42 }
43 
44 -(void) setScore:(float)fl
45 {
46     _score = fl;
47 }
48 
49 -(float) score
50 {
51     return _score;
52 }
53 
54 @end
View Code
 1 #import <Foundation/Foundation.h>
 2 #import "Student.h"
 3 #import "Course.h"
 4 
 5 int main(int argc, const char * argv[]) {
 6     @autoreleasepool {
 7        
 8         //alloc方法创建实例空间,init初始化
 9         Course *c = [[Course alloc] initWithName:@"叶小钗" andScore:90];
10         
11         NSLog(@"%@, %f", c.name, c.score);
12         
13     }
14     return 0;
15 }

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

  1 #import <Foundation/Foundation.h>
  2 
  3 @interface Course : NSObject
  4 {
  5     NSString *_name;
  6     float _score;
  7 }
  8 
  9 -(instancetype)initWithName:(NSString *)newName andScore:(float)newScore;
 10 
 11 -(void)setName: (NSString *)str;
 12 -(NSString *)name;
 13 
 14 -(void)setScore: (float)fl;
 15 -(float)score;
 16 
 17 -(void)showData;
 18 
 19 @end
 20 
 21 #import "Course.h"
 22 
 23 @implementation Course
 24 
 25 //自定义构造方法
 26 -(instancetype)initWithName:(NSString *)newName andScore:(float)newScore
 27 {
 28     self = [super init];
 29     if (self) {
 30         _name = newName;
 31         _score = newScore;
 32     }
 33     return self;
 34 }
 35 
 36 -(void) setName:(NSString *)str
 37 {
 38     _name = str;
 39 }
 40 
 41 -(NSString *) name
 42 {
 43     return _name;
 44 }
 45 
 46 -(void) setScore:(float)fl
 47 {
 48     _score = fl;
 49 }
 50 
 51 -(float) score
 52 {
 53     return _score;
 54 }
 55 
 56 -(void) showData
 57 {
 58     NSLog(@"课程名:%@", _name);
 59     NSLog(@"课程得分:%f",_score);
 60 }
 61 
 62 @end
 63 
 64 #import <Foundation/Foundation.h>
 65 #import "Course.h"
 66 
 67 @interface Student : NSObject
 68 {
 69     NSString *_name;
 70     int _age;
 71     NSString *_sex;
 72     Course *_chinese;
 73     Course *_math;
 74     //录入时间
 75     NSDate *_dateCreate;
 76 }
 77 
 78 -(instancetype)initWithName:(NSString *)newName andAge:(int)newAge andSex:(NSString *)newSex andChinese:(Course *) newChinese andMath:(Course *) newMath;
 79 
 80 -(void)setName: (NSString *)str;
 81 -(NSString *)name;
 82 
 83 -(void)setAge: (int)a;
 84 -(int)age;
 85 
 86 -(void)setSex: (NSString *)str;
 87 -(NSString *)sex;
 88 
 89 -(void)setChinese: (Course *)c;
 90 -(Course *)chinese;
 91 
 92 -(void)setMath: (Course *)c;
 93 -(Course *)math;
 94 
 95 //只暴露读取接口
 96 -(NSDate *)dateCreate;
 97 
 98 -(void) showData;
 99 
100 @end
101 
102 #import "Student.h"
103 
104 @implementation Student
105 
106 -(instancetype)initWithName:(NSString *)newName andAge:(int)newAge andSex:(NSString *)newSex andChinese:(Course *) newChinese andMath:(Course *) newMath
107 {
108     self = [super init];
109     if (self) {
110         _name = newName;
111         _age = newAge;
112         _sex = newSex;
113         _chinese = newChinese;
114         _math = newMath;
115         _dateCreate = [[NSDate alloc] init];
116     }
117     return self;
118   }
119 
120 -(void) setName:(NSString *)str
121 {
122     _name = str;
123 }
124 
125 -(NSString *) name
126 {
127     return _name;
128 }
129 
130 -(void)setAge: (int)a
131 {
132     _age = a;
133 }
134 
135 -(int)age
136 {
137     return _age;
138 }
139 
140 -(void)setSex: (NSString *)str
141 {
142     _sex = str;
143 }
144 
145 -(NSString *)sex
146 {
147     return _sex;
148 }
149 
150 -(void)setChinese: (Course *)c
151 {
152     _chinese = c;
153 }
154 
155 -(Course *)chinese
156 {
157     return _chinese;
158 }
159 
160 -(void)setMath: (Course *)c
161 {
162     _math = c;
163 }
164 
165 -(Course *)math
166 {
167     return _math;
168 }
169 
170 //只暴露读取接口
171 -(NSDate *)dateCreate
172 {
173     return _dateCreate;
174 }
175 
176 -(void) showData
177 {
178     NSLog(@"姓名:%@", _name);
179     NSLog(@"性别:%@", _sex);
180     NSLog(@"年龄:%d", _age);
181     [_chinese showData];
182     [_math showData];  
183     
184 }
185 
186 
187 @end
View Code
 1 #import <Foundation/Foundation.h>
 2 #import "Student.h"
 3 #import "Course.h"
 4 
 5 int main(int argc, const char * argv[]) {
 6     @autoreleasepool {
 7        
 8         //alloc方法创建实例空间,init初始化
 9         Course *chinese = [[Course alloc] initWithName:@"语文" andScore:90];
10         Course *math = [[Course alloc] initWithName:@"数学" andScore:95];
11         
12         Student *s = [[Student alloc] initWithName:@"叶小钗" andAge:27 andSex:@"" andChinese:chinese andMath:math];
13         
14         [s showData];
15         
16     }
17     return 0;
18 }
2015-08-06 23:42:24.853 student[3394:246243] 姓名:叶小钗
2015-08-06 23:42:24.854 student[3394:246243] 性别:男
2015-08-06 23:42:24.854 student[3394:246243] 年龄:27
2015-08-06 23:42:24.855 student[3394:246243] 课程名:语文
2015-08-06 23:42:24.855 student[3394:246243] 课程得分:90.000000
2015-08-06 23:42:24.855 student[3394:246243] 课程名:数学
2015-08-06 23:42:24.855 student[3394:246243] 课程得分:95.000000
Program ended with exit code: 0

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值