当前位置:首页 > 操作系统 > Ios

【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>
23@interface Student : NSObject
45@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;
 7int _age;
 8     NSString *_sex;
 9     Course *_chinese;
10     Course *_math;
11//录入时间12     NSDate *_dateCreate;
13}
14@end

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

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

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

技术分享 技术分享
                  1
                #import <Foundation/Foundation.h>
  2  3@interface Course : NSObject
  4{
  5     NSString *_name;
  6float _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{
 28return _name;
 29}
 30 31 -(void) setScore:(float)fl
 32{
 33     _score = fl;
 34}
 35 36 -(float) score
 37{
 38return _score;
 39}
 40 41@end 42 43#import <Foundation/Foundation.h>
 44#import"Course.h" 45 46@interface Student : NSObject
 47{
 48     NSString *_name;
 49int _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{
 88return _name;
 89}
 90 91 -(void)setAge: (int)a
 92{
 93     _age = a;
 94}
 95 96 -(int)age
 97{
 98return _age;
 99}
100101 -(void)setSex: (NSString *)str
102{
103     _sex = str;
104}
105106 -(NSString *)sex
107{
108return _sex;
109}
110111 -(void)setChinese: (Course *)c
112{
113     _chinese = c;
114}
115116 -(Course *)chinese
117{
118return _chinese;
119}
120121 -(void)setMath: (Course *)c
122{
123     _math = c;
124}
125126 -(Course *)math
127{
128return _math;
129}
130131//只暴露读取接口132 -(NSDate *)dateCreate
133{
134return _dateCreate;
135}
136137@end
Student-Course

构造函数

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

技术分享 技术分享
                 1
                #import <Foundation/Foundation.h>
 2 3@interface Course : NSObject
 4{
 5     NSString *_name;
 6float _score;
 7}
 8 9 -(instancetype)initWithName:(NSString *)newName andScore:(float)newScore;
1011 -(void)setName: (NSString *)str;
12 -(NSString *)name;
1314 -(void)setScore: (float)fl;
15 -(float)score;
1617@end1819#import"Course.h"2021@implementation Course
2223//自定义构造方法24 -(instancetype)initWithName:(NSString *)newName andScore:(float)newScore
25{
26     self = [super init];
27if (self) {
28         _name = newName;
29         _score = newScore;
30    }
31return self;
32}
3334 -(void) setName:(NSString *)str
35{
36     _name = str;
37}
3839 -(NSString *) name
40{
41return _name;
42}
4344 -(void) setScore:(float)fl
45{
46     _score = fl;
47}
4849 -(float) score
50{
51return _score;
52}
5354@end
View Code
             1
            #import <Foundation/Foundation.h>
 2#import"Student.h" 3#import"Course.h" 4 5int main(int argc, constchar * argv[]) {
 6    @autoreleasepool {
 7 8//alloc方法创建实例空间,init初始化 9         Course *c = [[Course alloc] initWithName:@"叶小钗" andScore:90];
1011         NSLog(@"%@, %f", c.name, c.score);
1213    }
14return0;
15 }

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

技术分享 技术分享
                  1
                #import <Foundation/Foundation.h>
  2  3@interface Course : NSObject
  4{
  5     NSString *_name;
  6float _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];
 29if (self) {
 30         _name = newName;
 31         _score = newScore;
 32    }
 33return self;
 34}
 35 36 -(void) setName:(NSString *)str
 37{
 38     _name = str;
 39}
 40 41 -(NSString *) name
 42{
 43return _name;
 44}
 45 46 -(void) setScore:(float)fl
 47{
 48     _score = fl;
 49}
 50 51 -(float) score
 52{
 53return _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;
 70int _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;
 99100@end101102#import"Student.h"103104@implementation Student
105106 -(instancetype)initWithName:(NSString *)newName andAge:(int)newAge andSex:(NSString *)newSex andChinese:(Course *) newChinese andMath:(Course *) newMath
107{
108     self = [super init];
109if (self) {
110         _name = newName;
111         _age = newAge;
112         _sex = newSex;
113         _chinese = newChinese;
114         _math = newMath;
115         _dateCreate = [[NSDate alloc] init];
116    }
117return self;
118  }
119120 -(void) setName:(NSString *)str
121{
122     _name = str;
123}
124125 -(NSString *) name
126{
127return _name;
128}
129130 -(void)setAge: (int)a
131{
132     _age = a;
133}
134135 -(int)age
136{
137return _age;
138}
139140 -(void)setSex: (NSString *)str
141{
142     _sex = str;
143}
144145 -(NSString *)sex
146{
147return _sex;
148}
149150 -(void)setChinese: (Course *)c
151{
152     _chinese = c;
153}
154155 -(Course *)chinese
156{
157return _chinese;
158}
159160 -(void)setMath: (Course *)c
161{
162     _math = c;
163}
164165 -(Course *)math
166{
167return _math;
168}
169170//只暴露读取接口171 -(NSDate *)dateCreate
172{
173return _dateCreate;
174}
175176 -(void) showData
177{
178     NSLog(@"姓名:%@", _name);
179     NSLog(@"性别:%@", _sex);
180     NSLog(@"年龄:%d", _age);
181    [_chinese showData];
182    [_math showData];  
183184}
185186187@end
View Code
             1
            #import <Foundation/Foundation.h>
 2#import"Student.h" 3#import"Course.h" 4 5int main(int argc, constchar * argv[]) {
 6    @autoreleasepool {
 7 8//alloc方法创建实例空间,init初始化 9         Course *chinese = [[Course alloc] initWithName:@"语文" andScore:90];
10         Course *math = [[Course alloc] initWithName:@"数学" andScore:95];
1112         Student *s = [[Student alloc] initWithName:@"叶小钗" andAge:27 andSex:@"" andChinese:chinese andMath:math];
1314        [s showData];
1516    }
17return0;
18 }
            2015-08-0623:42:24.853 student[3394:246243] 姓名:叶小钗
2015-08-0623:42:24.854 student[3394:246243] 性别:男
2015-08-0623:42:24.854 student[3394:246243] 年龄:272015-08-0623:42:24.855 student[3394:246243] 课程名:语文
2015-08-0623:42:24.855 student[3394:246243] 课程得分:90.0000002015-08-0623:42:24.855 student[3394:246243] 课程名:数学
2015-08-0623:42:24.855 student[3394:246243] 课程得分:95.000000
Program ended with exit code: 0

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

原文:http://www.cnblogs.com/yexiaochai/p/4600076.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!