【练习】成绩

/*

成绩类:

C语言成绩(可读可写)

OC成绩(可读可写)

总分(只读)

平均分(只读)

*/


main.h

#import

#import "Score.h"

int main(int argc, const char * argv[])

{

Score *c = [Score new];


[c setOcScore:90];

int b = [c ocScore];

NSLog(@"oc=%d",b);


[c setCScore:96];

int d = [c ocScore];

NSLog(@"c=%d",d);


int e = [c allScore];

NSLog(@"allScore=%d",e);


int f = [c aveScore];

NSLog(@"aveScore=%d",f);


return 0;

}


Score.h

#import

@interface Score : NSObject

{

int _cScore;

int _ocScore;


int _allScore;

int _aveScore;

}


- (void)setCScore:(int)cScore;

- (int)cScore;


- (void)setOcScore:(int)ocScore;

- (int)ocScore;


- (int)allScore;


- (int)aveScore;


@end


Score.m

#import

@implementation Score

- (void)setCScore:(int)cScore

{

_cScore = cScore;

 _allScore = _cScore+_ocScore;

_aveScore = _allScore/2;

//

}


- (int)cScore

{

return _cScore;

}


- (void)setOcScore:(int)ocScore

{

_ocScore = ocScore;

}


- (int)ocScore

{

return _ocScore;

}


- (int)allScore

{

//  return _cScore+_ocScore;

//  不这样写,每调用一次方法就要完成一次加法

return _allScore;

}


- (int)aveScore

{

//  return (_cScore + _ocScore)/2;

return _aveScore;

//  return [self allScore]/2;

}

@end

你可能感兴趣的:(【练习】成绩)