- #import <Foundation/Foundation.h>
- typedef enum dogclore{white,block,yello,green,purble,gold}Se;
- typedef enum dogsex{male,female}xb;
- //定义一个狗类(类的声明)
- @interface Dog:NSObject
- {
- //类的属性
- @public
- Se _clore;
- int _speed;
- xb _sex;
- float _weight;
- }
- //类的行为
- -(void)eat:(NSString *)food;//有参函数的声明
- -(void)bark;//无参函数的声明
- -(void)run;
- -(BOOL)fclore:(Dog *)sclore;
- -(int)walk:(Dog *)speed;
- @end
- //类的实现
- @implementation Dog
- -(void)eat:(NSString *)food{
- _weight+=0.5;
- NSLog(@"%.2f",_weight);
- }
- -(void)bark{
- NSLog(@"颜色:%d速度:%d性别:%d体重%.2f",_clore,_speed,_sex,_weight);
- }
- -(void)run{
- _weight-=0.5;
- NSLog(@"速度:%d,体重:%.2f",_speed,_weight);
- }
- -(BOOL)fclore:(Dog *)sclore{
- if(_clore==sclore->_clore){
- return YES;
- }else{
- return NO;
- }
-
- }
- -(int)walk:(Dog *)speed{
-
- return _speed-speed->_speed;
- }
- @end
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- // 创建一个对象
- Dog *p=[Dog new];
- //访问实体变量
- p -> _clore=block;
- p -> _speed=60;
- p -> _sex=male;
- p -> _weight=12.4f;
-
- //再创建一个对象
- Dog *p1=[Dog new];
- p1 -> _clore=white;
- p1 -> _speed = 45;
- //方法的调用
- [p eat:@"狗在吃香肠"];
- [p bark];
- [p run];
- BOOL str=[p fclore:p1];
- int n=[p walk:p1];
- NSLog(@"%d %d",str,n);
-
- NSLog(@"%d",p1->_clore);
-
- }
- return 0;
- }
复制代码 |