-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
#include<Foundation/Foundation.h>
//狗的颜色枚举值
typedef enum {
ColorRed,
ColorBlack,
ColorWrite,
ColorGreen
} Color ;
//狗的性别枚举值
typedef enum {
SexBoy,
SexGirle,
SexUnknown
} Sex ;
//声明Dog类
@interface Dog : NSObject
{
//狗的属性
@public
Color color ;
int speed ;
Sex sex ;
int weight ;
}
//狗的行为
- (void) setDogColor:(Color)DogColor setDogSpeed:(int)DogSpeed setDogSex:(Sex)DogSex seDogtWeght:(int)DogWeght;
//设置狗的基本属性
- (void) showDog ;//显示狗的基本属性
- (void) eat ;//狗吃一次,体重增加3kg
- (void) run ;//狗跑一次,体重减少1kg
- (bool) compareColorWithOther : (Dog *) otherDogColor ;//比较颜色
- (bool) compareSpeedWithOther : (Dog *) otherDogSpeed ;//比较速度
@end
@implementation Dog
- (void) setDogColor:(Color)DogColor setDogSpeed:(int)DogSpeed setDogSex:(Sex)DogSex seDogtWeght:(int)DogWeght
{
color = DogColor ;
speed = DogSpeed ;
sex = DogSex ;
weight = DogWeght ;
}
- (void) showDog
{
NSLog(@"\ndogColor = %d ,\ndogSpeed = %d ,\ndogSex = %d ,\ndogWeight = %d"
,color,speed,sex,weight) ;
}
- (void) eat
{
weight +=3 ;
NSLog(@"吃了一顿,狗狗现在的体重是%d",weight) ;
}
- (void) run
{
weight -=1 ;
NSLog(@"跑了一顿,狗狗现在的体重是%d",weight) ;
}
- (bool) compareColorWithOther : (Dog *) otherDogColor
{
return color = otherDogColor->color ;
}
- (bool) compareSpeedWithOther:(Dog*)otherDogSpeed
{
return speed == otherDogSpeed->speed ;
}
@end
int main()
{
Dog *dog = [Dog new] ;
[dog setDogColor:ColorGreen setDogSpeed:20 setDogSex:SexGirle seDogtWeght:54] ;
/*
dog->color = ColorGreen ;
dog->speed = 20 ;
dog->sex = SexGirle ;
dog->weight = 54 ;
*/
[dog showDog] ;
[dog eat] ;
[dog run] ;
[dog eat] ;
return 0 ;
}