#import <Foundation/Foundation.h>
//定义两个枚举
typedef enum color{kblack,kwhite,kyellow} color;
typedef enum sex{kmale,kfemaile} sex;
//声明一个方法
@interface Dog : NSObject
{
@public
//定义成员变量(实例变量)(属性)
color _color;
sex _sex;
int _speed;
float _weight;
}
//声明行为
-(void)eat;
-(void)run;
-(int)speedcom:(Dog *)dog;
-(BOOL)colorcom:(Dog *)dog;
@end
//方法的实现
@implementation Dog
//行为实现
-(void)eat{
_weight+=0.5f;
NSLog(@"狗吃了,体重变为%.2f",_weight);
}
-(void)run{
_weight-=0.5f;
NSLog(@"狗绕地球跑了一圈,体重变为%.2f",_weight);
}
-(int)speedcom:(Dog *)dog{
return _speed-dog->_speed;
}
-(BOOL)colorcom:(Dog *)dog{
if (_color==dog->_color) {
return YES;
}
return NO;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *bd=[Dog new];
bd->_color=kblack;
bd->_sex=kmale;
bd->_speed=200;
bd->_weight=2.5f;
[bd eat];
[bd run];
Dog *wd=[Dog new];
wd->_color=kwhite;
wd->_sex=kfemaile;
wd->_speed=150;
wd->_weight=2.0f;
int num=[bd speedcom:wd];
NSLog(@"两只狗的速度差为:%d",num);
BOOL isyes=[bd colorcom:wd];
if (isyes) {
NSLog(@"两只狗的毛色相同");
}else{
NSLog(@"两只狗的毛色不同");
}
}
return 0;
}
|