黑马程序员---object-c知识点总结(二)封装,继承,多态

-----------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 ;

    

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值