#import <Foundation/Foundation.h>
typedef enum Sex{female,male} sSex;
@interface Student : NSObject
{
NSString *_name;
int _age;
sSex _sex;
int _grade;
}
-(void)setName:(NSString *)name;
-(void)setAge:(int)age;
-(void)setSex:(int)sex;
-(void)setGrade:(int)grade;
-(NSString *)getName;
-(int)getAge;
-(int)getSex;
-(int)getGrade;
@end
#import "Student.h"
@implementation Student
-(void)setName:(NSString *)name{
_name=name;
}
-(void)setAge:(int)age{
_age=age;
}
-(void)setSex:(int)sex{
if(sex==1||sex==0){
_sex=sex;
}
}
-(void)setGrade:(int)grade{
if (grade>=0&&grade<=100) {
_grade=grade;
}
}
-(NSString *)getName{
return _name;
}
-(int)getAge{
return _age;
}
-(int)getSex{
return _sex;
}
-(int)getGrade{
return _grade;
}
@end
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *ncr=[Student new];
[ncr setName:@"Nightcrawler"];
[ncr setAge:21];
[ncr setSex:male];
[ncr setGrade:100];
NSString *name=[ncr getName];
int age=[ncr getAge];
int sex=[ncr getSex];
int grade=[ncr getGrade];
NSString *ssex;
if (sex==1) {
ssex=@"male";
}else{
ssex=@"female";
}
NSLog(@"姓名:%@,年龄:%d,性别:%@,成绩:%d",name,age,ssex,grade);
}
return 0;
}
|