// main.m8,思考&实现:
//设计一个”狗“类
//1> 属性
//* 颜色
//* 速度(单位是m/s)
//* 性别
//* 体重(单位是kg)
//// OC-1
//
// Created by madhavan on 15/9/8.
// Copyright (c) 2015年 madjie. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person:NSObject
{ @public
NSString *name;
NSString *dog;
}
@end
@implementation Person
@end
#pragma mark-
//创建狗 类....狗类的声明
@interface Dog:NSObject
{
@public
//实例变量.成员变量.属性
NSString *_color;
int _speed;
NSString *_sex;
float _weight;
}
//对象方法,定义形参
-(void)eat:(NSString *)foodname;
-(void)wangWang;
-(void)run;
-(BOOL)biJiao:(Dog *)dog1;//传入对象
-(int)suDu:(Dog *)dog1speed;
@end
#pragma mark-
//类的实现
@implementation Dog
//行为 方法的具体实现
-(void)eat:(NSString *)foodname{
_weight += 0.5;
NSLog(@"吃的食物%@,当前体重%.2f",foodname,_weight);
}
-(void)wangWang{
NSLog(@"狗狗信息:%@,%d,%@,%.2f",_color,_speed,_sex,_weight);
}
-(void)run{
_weight -= 0.5;
NSLog(@"速度是%d,跑完体重%.2f",_speed,_weight);
}
-(BOOL)biJiao:(Dog *)dog1{
if(_color == dog1->_color){
return YES;
}
return NO;
}
-(int)suDu:(Dog *)dog1{
return _speed - dog1->_speed;
}
@end
int main(){
@autoreleasepool {
Dog *dog = [Dog new];
dog->_color = @"白色";
dog->_speed = 34;
dog->_sex = @"母狗狗";
dog->_weight = 40.5;
[dog wangWang];
[dog eat:@"一根大骨头"];
[dog run];
Dog *dog1 = [[Dog alloc]init];
dog1->_color = @"黑色";
dog1->_speed = 24;
dog1->_sex = @"公狗狗";
dog1->_weight = 35.5;
[dog1 wangWang];
[dog1 eat:@"两根骨头"];
[dog1 run];
int a = [dog biJiao:dog1];
NSLog(@"%d",a);
int b = [dog suDu:dog1];
NSLog(@"%d",b);
}
} |
|