黑马程序员技术交流社区
标题:
OC练习
[打印本页]
作者:
yufanyufan77
时间:
2015-10-26 13:30
标题:
OC练习
本帖最后由 yufanyufan77 于 2015-10-26 14:52 编辑
#import<Foundation/Foundation.h>
typedef enum
{
yellow,
white,
colours
}col;
typedef enum
{
male,
female//雌
}SEX;
#pragma mark 狗类
@interface Dog:NSObject
{
@public
col _colour;
int _speed;
float _weight;
SEX _sex;
}
-(void)eat:(int) counts;
-(void)run: (int) steeps;
-(void)bark;
//比较颜色
-(boolean)compColour:(Dog*)x ;
//比较性别
-(boolean)compSex:(Dog*) x ;
@end
@implementation Dog
-(void)eat:(int) counts
{
// 没吃一次饭长0.5Kg
_weight += 0.5f;
NSLog(@"现在体重是:%f",_weight);
}
-(void)run: (int) steeps
{
// 每跑100步体重减轻0.5Kg
_weight -= steeps/100 * 0.5f;
NSLog(@"现在体重是:%f",_weight);
}
-(void)bark
{
NSLog(@"颜色是:%d \n体重是%fKg \n性别是:%d \n速度是%dm/s\n",_colour,_weight,_sex,_speed);
}
//比较颜色
-(BOOL)compColour:(Dog*)x
{
if(x->_colour == _colour)
{
return NO;
}
else
return YES;
}
//比较性别
-(BOOL)compSex:(Dog*)x
{
if(x->_sex == _sex)
{
return NO;
}
else
return YES;
}
@end
int main(int argc , char**args)
{
NSLog(@"第一只狗");
Dog* dog1 = [Dog new];
dog1->_weight = 20.0f;
dog1->_colour = yellow;
dog1->_sex = female;
dog1->_speed = 100;
//[dog1 bark];
//[dog1 eat:100];
//[dog1 run:560];
printf("\n\n");
NSLog(@"第二只狗");
Dog* dog2 = [Dog new];
dog2->_weight = 10.0f;
dog2->_colour = white;
dog2->_sex = female;
dog2->_speed = 100;
//[dog2 bark];
//[dog2 eat:500];
//[dog2 run:1000];
NSLog(@"%i",[dog1 compSex:dog2]);
NSLog(@"%i",[dog1 compColour:dog2]);
while(1);
return 0;
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2