黑马程序员技术交流社区
标题:
请问类的组合时候怎么对被组合类里面的属性进行赋值
[打印本页]
作者:
chaoren
时间:
2015-3-12 03:26
标题:
请问类的组合时候怎么对被组合类里面的属性进行赋值
复制代码
对于student类继承了person类,组合了score类,请问我怎么对学生里面的score进行赋值?有没有大神指导下
作者:
chaoren
时间:
2015-3-12 03:28
#import <Foundation/Foundation.h>
typedef struct Date{
int year;
int month;
int day
};
typedef enum {
sexMan,
sexWoman
}Sex;
@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
@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()
{
return 0;
}
复制代码
作者:
chaoren
时间:
2015-3-12 03:29
相关代码如上,求指导
作者:
chaoren
时间:
2015-3-12 13:36
#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;
}
复制代码
已经实现了,不知道代码是否规范合乎逻辑
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2