Objective-C - 动态方法、静态属性实例

设计”狗”类

/*
 设计一个”狗“类
 1> 属性
 * 颜色
 * 速度(单位是m/s)
 * 性别
 * 体重(单位是kg)

 2> 行为
 * 吃:每吃一次,体重增加0.5kg,输出吃完后的体重
 * 吠(叫):输出所有的属性
 * 跑:每跑一次,体重减少0.5kg,输出速度和跑完后的体重
 * 比较颜色:跟别的狗比较颜色,如果一样,返回YES,不一样,返回NO
 * 比较速度:跟别的狗比较速度,返回速度差(自己的速度 - 其他狗的速度)
*/

#import <Foundation/Foundation.h>

// 颜色
typedef enum {
    ColorBlack,
    ColorGreen,
    ColorBlue,
    ColoeRed
} Color;

// 性别
typedef enum {
    SexMan,
    SexWoman,
    SecUnknow
} Sex;

@interface Dog : NSObject
{
    @public
    Color color; // 颜色
    int speed; // 速度
    Sex sex; // 性别
    double weight; // 体重
}

- (void)eat; // 吃
- (void)bark; // 吠
- (void)run; // 跑
- (BOOL)isSameColorWithOther:(Dog *)other; // 比较是否为相同颜色
- (int)compareSpeedWithOther:(Dog *)other; // 比较速度差
@end

@implementation Dog
- (void)eat // 吃
{
    weight += 0.5;
    NSLog(@"狗吃完后的体重是%f公斤", weight);
}
- (void)bark // 吠
{
    NSLog(@"狗的颜色是%d,速度为%dkm/h,性别是%d,体重是%f公斤", color, speed, sex, weight);
}
- (void)run // 跑
{
    weight -= 0.5;
    NSLog(@"狗跑完后的体重是%f公斤", weight);
}
- (BOOL)isSameColorWithOther:(Dog *)other // 比较是否为相同颜色
{
    return color == other->color;
}
- (int)compareSpeedWithOther:(Dog *)other // 比较速度差
{
    return speed - other->speed;
}
@end

设计”人”类

/*
 2.结合前面的“狗”类,设计一个“人”类
 1> 属性
 * 姓名
 * 狗(养了一条狗)

 2> 行为
 * 喂狗:每喂一次,狗就会执行“吃”这个行为
 * 遛狗:每溜一次,狗就会执行“跑”这个行为
*/
#import <Foundation/Foundation.h>

// 颜色
typedef enum {
    ColorBlack,
    ColorGreen,
    ColorBlue,
    ColoeRed
} Color;

// 性别
typedef enum {
    SexMan,
    SexWoman,
    SecUnknow
} Sex;

/*********************狗********************/
@interface Dog : NSObject
{
@public
    Color color; // 颜色
    int speed; // 速度
    Sex sex; // 性别
    double weight; // 体重
}

- (void)eat; // 吃
- (void)bark; // 吠
- (void)run; // 跑
- (BOOL)isSameColorWithOther:(Dog *)other; // 比较是否为相同颜色
- (int)compareSpeedWithOther:(Dog *)other; // 比较速度差
@end

@implementation Dog
- (void)eat // 吃
{
    weight += 0.5;
    NSLog(@"狗吃完后的体重是%f公斤", weight);
}
- (void)bark // 吠
{
    NSLog(@"狗的颜色是%d,速度为%dkm/h,性别是%d,体重是%f公斤", color, speed, sex, weight);
}
- (void)run // 跑
{
    weight -= 0.5;
    NSLog(@"狗跑完后的体重是%f公斤", weight);
}
- (BOOL)isSameColorWithOther:(Dog *)other // 比较是否为相同颜色
{
    return color == other->color;
}
- (int)compareSpeedWithOther:(Dog *)other // 比较速度差
{
    return speed - other->speed;
}
@end

/*********************人********************/
@interface Person : NSObject
{
    @public
    char *name; // 姓名
    Dog *dog; // 狗
}
- (void)walkDog; // 遛狗
- (void)feedDog; // 喂狗
@end

@implementation Person
- (void)walkDog // 遛狗
{
    [dog run];
}
- (void)feedDog // 喂狗
{
    [dog eat];
}
@end

设计”学生”类

/*
 3.设计一个”学生“类
 1> 属性
 * 姓名
 * 生日
 * 年龄
 * 身高(单位是m)
 * 体重(单位是kg)
 * 性别
 * C语言成绩
 * OC成绩
 * iOS成绩

 2> 行为
 * 跑步:每跑步一次,身高增加1cm,体重减小0.5kg,输出跑完后的体重
 * 吃饭:每吃一次,身高增加1cm,体重增加0.5kg,输出吃完后的体重
 * 学习:每学习一次,3可成绩各加1分,输出学习完后的3科成绩
 * 睡觉:输出所有的属性
 * 比较C语言成绩:跟另外一个学生比较C语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较OC成绩:跟另外一个学生比较OC语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较iOS成绩:跟另外一个学生比较iOS语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 计算总分:算出3科成绩的总分
 * 计算平均分:算出3科成绩的平均分
*/

#import <Foundation/Foundation.h>
// 颜色
typedef struct {
    int year;
    int month;
    int day;
} Date;

// 性别
typedef enum {
    SexMan,
    SexWoman,
    SecUnknow
} Sex;

@interface Student : NSObject
{
    @public
    char *name; // 姓名
    Date birthday; // 生日
    int age; // 年龄
    double height; // 身高
    double weight; // 体重
    Sex sex; // 性别
    int cScore; // C语言成绩
    int ocScore; // OC成绩
    int iosScore; // iOS成绩
}

- (void)run; // 跑
- (void)eat; // 吃
- (void)study; // 学习
- (void)sleep; // 睡觉
- (int)compareCScoreWithOther:(Student *)other; // 比较C成绩
- (int)compareOcScoreWithOther:(Student *)other; // 比较OC成绩
- (int)compareIosScoreWithOther:(Student *)other; // 比较iOS成绩
- (int)totalScore; // 总分
- (int)averageScore; // 平均分
@end

@implementation Student
- (void)run // 跑
{
    height += 0.01;
    weight -= 0.5;
    NSLog(@"学生跑完后的体重是%f公斤", weight);
}
- (void)eat // 吃
{
    height += 0.01;
    weight += 0.5;
    NSLog(@"学生吃完后的体重是%f公斤", weight);
}
- (void)study // 学习
{
    cScore += 1;
    ocScore += 1;
    iosScore += 1;
}
- (void)sleep // 睡觉
{
    NSLog(@"姓名=%s,身高=%f米,体重=%f公斤,生日=%d-%d-%d,年龄=%d岁,性别=%d,C成绩=%d,OC成绩=%d,iOS成绩=%d", name, height, weight, birthday.year, birthday.month, birthday.day, age, sex,
          cScore, ocScore, iosScore);
}
- (int)compareCScoreWithOther:(Student *)other // 比较C成绩
{
    return cScore - other->cScore;
}
- (int)compareOcScoreWithOther:(Student *)other // 比较OC成绩
{
    return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Student *)other // 比较iOS成绩
{
    return iosScore - other->iosScore;
}
- (int)totalScore // 总分
{
    return cScore + ocScore + iosScore;
}
- (int)averageScore // 平均分
{
    return (cScore + ocScore + iosScore) / 3;
}
@end

设计”成绩”类

/*
 4.设计一个成绩类
 1> 属性
 * C语言成绩
 * OC成绩
 * iOS成绩

 2> 行为
 * 比较C语言成绩:跟另外一个成绩对象比较C语言成绩,返回成绩差(自己 - 其他成绩)
 * 比较OC成绩:跟另外一个成绩对象比较OC语言成绩,返回成绩差(自己 - 其他成绩)
 * 比较iOS成绩:跟另外一个成绩对象比较iOS语言成绩,返回成绩差(自己 - 其他成绩)
 * 计算总分:算出3科成绩的总分
 * 计算平均分:算出3科成绩的平均分
*/

#import <Foundation/Foundation.h>

@interface Score : NSObject
{
    @public
    int cScore; // C语言成绩
    int ocScore; // OC成绩
    int iosScore; // iOS成绩
}

- (int)compareCScoreWithOther:(Score *)other; // 比较C成绩
- (int)compareOcScoreWithOther:(Score *)other; // 比较OC成绩
- (int)compareIosScoreWithOther:(Score *)other; // 比较iOS成绩
- (int)totalScore; // 总分
- (int)averageScore; // 平均分
@end

@implementation Score
- (int)compareCScoreWithOther:(Score *)other // 比较C成绩
{
    return cScore - other->cScore;
}
- (int)compareOcScoreWithOther:(Score *)other // 比较OC成绩
{
    return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Score *)other // 比较iOS成绩
{
    return iosScore - other->iosScore;
}
- (int)totalScore // 总分
{
    return cScore + ocScore + iosScore;
}
- (int)averageScore // 平均分
{
    return (cScore + ocScore + iosScore) / 3;
}
@end
/*
 5.利用前面的成绩类,重新设计一个学生类
 1> 属性
 * 姓名
 * 学号
 * 成绩(包括3科成绩)

 2> 行为
 * 比较C语言成绩:跟另外一个学生比较C语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较OC成绩:跟另外一个学生比较OC语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较iOS成绩:跟另外一个学生比较iOS语言成绩,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较总分:跟另外一个学生比较总分,返回成绩差(自己的成绩 - 其他人的成绩)
 * 比较平均分:跟另外一个学生比较平均分,返回成绩差(自己的成绩 - 其他人的成绩)
*/

#import <Foundation/Foundation.h>
/****************成绩******************/
@interface Score : NSObject
{
    @public
    int cScore; // C语言成绩
    int ocScore; // OC成绩
    int iosScore; // iOS成绩
}

- (int)compareCScoreWithOther:(Score *)other; // 比较C成绩
- (int)compareOcScoreWithOther:(Score *)other; // 比较OC成绩
- (int)compareIosScoreWithOther:(Score *)other; // 比较iOS成绩
- (int)totalScore; // 总分
- (int)averageScore; // 平均分
@end

@implementation Score
- (int)compareCScoreWithOther:(Score *)other // 比较C成绩
{
    return cScore - other->cScore;
}
- (int)compareOcScoreWithOther:(Score *)other // 比较OC成绩
{
    return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Score *)other // 比较iOS成绩
{
    return iosScore - other->iosScore;
}
- (int)totalScore // 总分
{
    return cScore + ocScore + iosScore;
}
- (int)averageScore // 平均分
{
    return (cScore + ocScore + iosScore) / 3;
}
@end

/********************学生********************/
@interface Student : NSObject
{
    @public
    char *name; // 姓名
    int no; // 学号
    Score *score; // 成绩
}

- (int)compareCScoreWithOther:(Student *)other; // 比较C成绩
- (int)compareOcScoreWithOther:(Student *)other; // 比较OC成绩
- (int)compareIosScoreWithOther:(Student *)other; // 比较iOS成绩
- (int)compareTotalScoreWithOther:(Student *)other; // 比较总成绩
- (int)compareAverageScoreWithOther:(Student *)other; // 比较平均成绩
@end

@implementation Student
- (int)compareCScoreWithOther:(Student *)other // 比较C成绩
{
    return [score compareCScoreWithOther:other->score];
}
- (int)compareOcScoreWithOther:(Student *)other // 比较OC成绩
{
    return [score compareOcScoreWithOther:other->score];
}
- (int)compareIosScoreWithOther:(Student *)other // 比较iOS成绩
{
    return [score compareIosScoreWithOther:other->score];
}
- (int)compareTotalScoreWithOther:(Student *)other // 比较总成绩
{
    return [score totalScore] - [other->score totalScore];
}
- (int)compareAverageScoreWithOther:(Student *)other // 比较平均成绩
{
    return [score averageScore] - [other->score averageScore];
}
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值