OC中封装与多态的实例

来源:互联网 发布:opentsdb java开发 编辑:程序博客网 时间:2024/05/31 19:10
/* 设计一个成绩类 属性:C语言成绩 OC成绩 IOS成绩 行为:    比较C语言成绩,返回自己与其他成绩差        比较OC成绩、比较IOS成绩、计算总分、计算平均分 */#import <Foundation/Foundation.h>@interface YObject : NSObject-(void)printClassName;@end@implementation YObject-(void)printClassName{    NSLog(@"%@",[self class]);}@end//定义成绩类@interface ChengJi : YObject{    //属性        //c语言成绩    int _cScore ;        //oc成绩    int _ocSocre ;        //ios成绩    int _iosScore;    }@property int cScore;@property int ocScore;@property int iosScore;//比较C语言成绩-(int) comparisonCWith:(ChengJi*)chengJi;//比较OC语言成绩-(int) comparisonOcWith:(ChengJi*)chengJi;//比较IOS成绩-(int) comparisonIosWith:(ChengJi*)chengJi;//计算总分-(int)getSumScore;//计算平均分-(int)getAverageScore;@end@implementation ChengJi@synthesize cScore = _cScore;@synthesize ocScore = _ocSocre;@synthesize iosScore = _iosScore;-(int) comparisonCWith:(ChengJi*)chengJi{    return self.cScore - chengJi.cScore;}-(int) comparisonOcWith:(ChengJi*)chengJi{    return self.ocScore - chengJi.ocScore;}-(int) comparisonIosWith:(ChengJi*)chengJi{    return self.iosScore - chengJi.iosScore;}//计算总分-(int)getSumScore{    return self.cScore+self.ocScore+self.iosScore;}//计算平均分-(int)getAverageScore{    return [self getSumScore]/3;}@endint main(){        ChengJi *chengJi1 = [ChengJi new];    chengJi1.cScore = 100;    chengJi1.ocScore = 100;    chengJi1.iosScore = 100;    ChengJi *chengJi2 = [ChengJi new];    chengJi2.cScore = 90;    chengJi2.ocScore = 80;    chengJi2.iosScore = 70;        [chengJi1 printClassName];    NSLog(@"第一个比第二个的C语言成绩高%d分",[chengJi1 comparisonCWith: chengJi2]);//    [ChengJi printClassName];    return 0;}

0 0
原创粉丝点击