A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

设计一个学生类,
属性有:姓名,年龄,性别,成绩;

顺便写出,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;
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马