设计一个学生类,
属性有:姓名,年龄,性别,成绩;
顺便写出,set方法和get方法的命名规范;
typedef enum{
kSexMan,
kSexWoman,
}Sex;
#import <Foundation/Foundation.h>
@interface Student : NSObject{
NSString *_name;
int _age;
Sex _sex;
int _score;
}
-(void)setName:(NSString *)name;
-(NSString *)name;
-(void)setAge:(int)age;
-(int)age;
-(void)setSex:(Sex)sex;
-(Sex)sex;
-(void)setScore:(int)score;
-(int)score;
@end
#import "Student.h"
@implementation Student
-(void)setName:(NSString *)name{
_name = name;
}
-(NSString *)name{
return _name;
}
-(void)setAge:(int)age{
_age = age;
}
-(int)age{
return _age;
}
-(void)setSex:(Sex)sex{
_sex = sex;
}
-(Sex)sex{
return _sex;
}
-(void)setScore:(int)score{
_score = score;
}
-(int)score{
return _score;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *stu = [Student new];
[stu setName:@"王五"];
[stu setAge:23];
[stu setSex:kSexWoman];
[stu setScore:99];
NSLog(@"姓名=%@,年龄= %d,性别= %d,分数=%d",[stu name],[stu age],[stu sex],[stu score]);
}
return 0;
}
|
|