要求设计一个类,要求有姓名,年龄,课程信息,和分数;其中课程信息包括课程名称,课程老师、上课时长。手动编写设置器和访问器方法(getter和setter)。
一、getter和setter
1. 概念
在OC里, 为实例变量赋值的方法称作setter(设置器)
读取实例变量值的方法称作getter(访问器)
引入这两个是对面向对象语言封装的最基本的支持。
2. 书写格式
setter的书写格式如下, -(void)setAge:(int)age;即set+首字母大写的实例变量名。
getter的书写格式如下,-(int) age; 即返回值类型与变量类型一致,方法名与实例变量名相同。
3. 与实例变量的关系
无论setter还是getter内部操作的是实例变量
每一个实例变量都需要一对setter和getter方法
4. 调用方法
一般的调用方法,是传统的带中括号[ ]的调用方法,比如
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #31595d}
-(void)printCourse{
NSLog(@"courseName =%@ courseTime = %li teacherNAme=%@",[self courseName],[self courseTime],[self teacherName]);
}
其中[self courseName] 就是对courseName这个getter方法的调用。
二、类结构的设计
看要求可知,分成两层结构比较简单,一层是学生,一层是课程,学生类中的一些实例变量属于课程类。
课程类中的变量应该包括:课程名称,老师名称,课程时长。
学生类包括:姓名,年龄,对应课程,分数。
需要注意的几点:
- 因为在Student类中用到了Course类,所以在Student.h文件中要import Course.h头文件。
- Course构造方法是指针,所以在给学生的课程赋值时要用指针赋值。
三、代码
#######Course.h文件
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s6 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}
#import <Foundation/Foundation.h>
@interface Course : NSObject
{
NSString *_courseName;
NSString *_teacherName;
NSInteger _courseTime;
}
//构造方法
-(id)initWithCourseName:(NSString *)courseName andCourseTime:(NSInteger)courseTime;
//getter方法
-(NSString *)courseName;
-(NSString *)teacherName;
-(NSInteger)courseTime;
//setter方法
-(void)setTeacherName:(NSString *)teacherName;
//打印课程
-(void)printCourse;
@end
#######Course.m文件
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s9 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s10 {font-variant-ligatures: no-common-ligatures; color: #31595d}
#import "Course.h"
@implementation Course
//构造方法
-(id)initWithCourseName:(NSString *)courseName andCourseTime:(NSInteger)courseTime
{
self=[super init];
if (self) {
_courseName=courseName;
_courseTime=courseTime;
}
return self;
}
//getter方法
-(NSString *)courseName
{
return _courseName;
}
-(NSString *)teacherName
{
return _teacherName;
}
-(NSInteger)courseTime
{
return _courseTime;
}
//setter方法
-(void)setTeacherName:(NSString *)teacherName
{
_teacherName=teacherName;
}
//打印课程
-(void)printCourse{
NSLog(@"courseName =%@ courseTime = %li teacherNAme=%@",[self courseName],[self courseTime],[self teacherName]);
}
@end
#######Student.h文件
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}
#import <Foundation/Foundation.h>
#import "Course.h"
@interface Student : NSObject
{
NSString *_name;
NSInteger _age;
NSInteger _score;
Course *_course1;
Course *_course2;
Course *_course3;
}
//构造方法
-(id)initWithName:(NSString *)name andAge:(NSInteger)age;
//getter方法 读取实际变量的值
-(NSString *)name;
-(NSInteger)age;
-(NSInteger)score;
//setter
-(void)setName:(NSString *)name andAge:(NSInteger)age;
//功能方法
-(NSString *)getAllTeacherNames;
-(NSString *)getAllCourseNames;
-(NSInteger)getAllCourseTimes;
-(void)setCourse:(Course *)aCourse;
//通过课程的索引设置课程
-(void)setCourse:(Course *)newCourse byIndex:(NSInteger)index;
//打印学生信息
-(void)printStudentInfo;
//类方法
+ (void)testStudent;
@end
#######Student.m文件
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #bb2ca2}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #31595d}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s10 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s11 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s12 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}
#import "Student.h"
@implementation Student
//构造方法
-(id)initWithName:(NSString *)name andAge:(NSInteger)age
{
self=[super init];
if(self){
_name =name;
_age =age;
}
return self;
}
//功能方法
-(NSString *)getAllCourseNames{
NSString *str=@"";
if (_course1) {
str=[str stringByAppendingString:[_course1 courseName]];
}
if (_course2) {
str=[str stringByAppendingString:[_course2 courseName]];
}
if(_course3){
str=[str stringByAppendingString:[_course3 courseName]];
}
return str;
}
-(NSString *)getAllTeacherNames
{
NSString *str=@"";
if (_course1) {
str=[str stringByAppendingString:[_course1 teacherName]];
}
if (_course2) {
str=[str stringByAppendingString:[_course2 teacherName]];
}
if (_course3) {
str=[str stringByAppendingString:[_course3 teacherName]];
}
return str;
}
-(NSInteger)getAllCourseTimes
{
NSInteger time=0;
if (_course1) {
time+=[_course1 courseTime];
}
if (_course2) {
time+=[_course2 courseTime];
}
if (_course3) {
time+=[_course3 courseTime];
}
return time;
}
-(void)setCourse:(Course *)newCourse byIndex:(NSInteger)index
{
if (index==1) {
_course1=newCourse;
}
else if (index==2) {
_course2=newCourse;
}
else if (index==3) {
_course3=newCourse;
}
}
//打印学生信息
-(void)printStudentInfo
{
NSLog(@"name=%@ age=%li score=%li",[self name],[self age],[self score]);
[_course1 printCourse];
[_course2 printCourse];
[_course3 printCourse];
}
//类方法
+(void)testStudent
{
Student *stu = [[Student alloc]initWithName:@"zhang" andAge:20];
Course *course1=[[Course alloc]initWithCourseName:@"chinese" andCourseTime:20];
[course1 setTeacherName:@"张老师"];
Course *course2=[[Course alloc]initWithCourseName:@"math" andCourseTime:30];
[course2 setTeacherName:@"杨老师"];
Course *course3=[[Course alloc]initWithCourseName:@"english" andCourseTime:40];
[course3 setTeacherName:@"王老师"];
stu->_course1=course1;
stu->_course2=course2;
[stu setCourse:course3 byIndex:2];
NSLog(@"%@",[stu getAllCourseNames]);
NSLog(@"%@",[stu getAllTeacherNames]);
NSLog(@"time = %li",[stu getAllCourseTimes]);
}
@end
四、 TODO
setter和getter的改进写法
参考:http://blog.csdn.net/lonelyroamer/article/details/7665112