设计”狗”类
#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
设计”人”类
#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
设计”学生”类
#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;
int ocScore;
int iosScore;
}
- (void)run;
- (void)eat;
- (void)study;
- (void)sleep;
- (int)compareCScoreWithOther:(Student *)other;
- (int)compareOcScoreWithOther:(Student *)other;
- (int)compareIosScoreWithOther:(Student *)other;
- (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
{
return cScore - other->cScore;
}
- (int)compareOcScoreWithOther:(Student *)other
{
return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Student *)other
{
return iosScore - other->iosScore;
}
- (int)totalScore
{
return cScore + ocScore + iosScore;
}
- (int)averageScore
{
return (cScore + ocScore + iosScore) / 3;
}
@end
设计”成绩”类
#import <Foundation/Foundation.h>
@interface Score : NSObject
{
@public
int cScore;
int ocScore;
int iosScore;
}
- (int)compareCScoreWithOther:(Score *)other;
- (int)compareOcScoreWithOther:(Score *)other;
- (int)compareIosScoreWithOther:(Score *)other;
- (int)totalScore;
- (int)averageScore;
@end
@implementation Score
- (int)compareCScoreWithOther:(Score *)other
{
return cScore - other->cScore;
}
- (int)compareOcScoreWithOther:(Score *)other
{
return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Score *)other
{
return iosScore - other->iosScore;
}
- (int)totalScore
{
return cScore + ocScore + iosScore;
}
- (int)averageScore
{
return (cScore + ocScore + iosScore) / 3;
}
@end
#import <Foundation/Foundation.h>
/****************成绩******************/
@interface Score : NSObject
{
@public
int cScore;
int ocScore;
int iosScore;
}
- (int)compareCScoreWithOther:(Score *)other;
- (int)compareOcScoreWithOther:(Score *)other;
- (int)compareIosScoreWithOther:(Score *)other;
- (int)totalScore;
- (int)averageScore;
@end
@implementation Score
- (int)compareCScoreWithOther:(Score *)other
{
return cScore - other->cScore;
}
- (int)compareOcScoreWithOther:(Score *)other
{
return ocScore - other->ocScore;
}
- (int)compareIosScoreWithOther:(Score *)other
{
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;
- (int)compareOcScoreWithOther:(Student *)other;
- (int)compareIosScoreWithOther:(Student *)other;
- (int)compareTotalScoreWithOther:(Student *)other;
- (int)compareAverageScoreWithOther:(Student *)other;
@end
@implementation Student
- (int)compareCScoreWithOther:(Student *)other
{
return [score compareCScoreWithOther:other->score];
}
- (int)compareOcScoreWithOther:(Student *)other
{
return [score compareOcScoreWithOther:other->score];
}
- (int)compareIosScoreWithOther:(Student *)other
{
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