- #import <Foundation/Foundation.h>
- //日期
- typedef struct {
- int year;
- int month;
- int day;
- }Date;
- //性别枚举
- typedef enum {
- sexMan,
- sexWoman
- }Sex;
- //score类申明
- @interface Score : NSObject
- {
- int _cScore;
- int _ocScore;
- int _iOSScore;
- }
- - (void)setCScore:(int)newCScore;
- - (int)cScore;
- - (int)ocScore;
- - (int)iOSScore;
- - (void)setOcScore:(int)newOcScore;
- - (void)setIosScore:(int)newIosScore;
- @end
- //score方法实现
- @implementation Score
- - (void)setCScore:(int)newCScore
- {
- _cScore = newCScore;
- }
- - (int)cScore
- {
- return _cScore;
- }
- - (int)ocScore
- {
- return _ocScore;
- }
- - (int)iOSScore
- {
- return _iOSScore;
- }
- - (void)setOcScore:(int)newOcScore
- {
- _ocScore = newOcScore;
- }
- - (void)setIosScore:(int)newIosScore
- {
- _iOSScore = newIosScore;
- }
- @end
- //人类声明
- @interface Person: NSObject
- {
- NSString *_name;//姓名
- Date _birthday;//生日
- int _age;//年龄
- double _weight;//体重
- double _height;//身高
- Sex _mySex;//性别
- }
- - (void)setName:(NSString *)newName;
- - (NSString *)name;
- - (void)setBirthday:(Date)newBirthday;
- - (Date)birthday;
- - (int)age;
- - (void)setWeight:(double)newWeight;
- - (double)weight;
- - (void)setHeight:(double)newHeight;
- - (double)height;
- - (void)setSex:(Sex)newSex;
- - (Sex)sex;
- @end
- //人类方法实现
- @implementation Person
- //set姓名
- - (void)setName:(NSString *)newName
- {
- _name = newName;
- }
- //get姓名
- - (NSString *)name
- {
- return _name;
- }
- //set生日
- - (void)setBirthday:(Date)newBirthday
- {
- _birthday.year = newBirthday.year;
- _birthday.month = newBirthday.month;
- _birthday.day = newBirthday.day;
- }
- //get生日
- - (Date)birthday
- {
- return _birthday;
- }
- //get年龄
- - (int)age
- {
- return 2015 - _birthday.year;
- // NSDate *date = [NSDate date];
- // NSCalendar *cal = [NSCalendar currentCalendar];
- // unsigned int unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
- // NSDateComponents *d = [cal components:unitFlags fromDate:date];//把要从date中获取的unitFlags标示的日期元素存放在NSDateComponents类型的d里面;
- // //然后就可以从d中获取具体的年月日了;
- // NSInteger year = [d year];
- // NSInteger month = [d month];
- // NSInteger day = [d day];
- //
- // NSLog(@"%ld",year);
- // NSLog(@"%ld",month);
- // NSLog(@"%ld",day);
- // _age =(int)year - _birthday.year;
- // if (((int)month - _birthday.month)<0) {
- // _age--;
- // }else{
- // if (<#condition#>) {
- // <#statements#>
- // }
- // }
- }
- //set体重
- - (void)setWeight:(double)newWeight
- {
- _weight = newWeight;
- }
- //get体重
- - (double)weight
- {
- return _weight;
- }
- //set身高
- - (void)setHeight:(double)newHeight
- {
- _height = newHeight;
- }
- //get身高
- - (double)height
- {
- return _height;
- }
- //set性别
- - (void)setSex:(Sex)newSex
- {
- _mySex = newSex;
- }
- //get性别
- - (Sex)sex
- {
- return _mySex;
- }
- @end
- //学生类:学生是人类,拥有成绩
- @interface Student : Person
- {
- Score *_score;//成绩。组合
- }
- - (void)setScore:(Score *)newScore;
- - (Score *)score;
- @end
- //学生类的方法实现
- @implementation Student
- - (void)setScore:(Score *)newScore
- {
- _score = newScore;//给成绩赋值
- }
- - (Score *)score//返回成绩
- {
- return _score;
- }
- @end
- int main()
- {
- Student *stu = [Student new];
- Score *sc = [Score new];
- [stu setName:@"张三"];
- [sc setCScore: 80];
- [sc setOcScore: 90];
- [sc setIosScore: 78];
- [stu setScore:sc];
- Score *newsc = [stu score];
- NSLog(@"Cscore=%d,iosscore=%d,osScore=%d",[newsc cScore],[newsc iOSScore],[newsc ocScore]);
- Date d = {1990,12,16};
- [stu setBirthday:d];
- NSLog(@"%@今年%d岁,他的的生日是%d-%d-%d",[stu name],[stu age],[stu birthday].year,[stu birthday].month,[stu birthday].day);
- return 0;
- }
复制代码
已经实现了,不知道代码是否规范合乎逻辑 |