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

-----------android培训java培训、java学习型技术博客、期待与您交流!------------ 

#import<Foundation/Foundation.h>


//动物颜色的枚举值

typedef enum {

    ColorRed ,

    ColorBlack ,

    ColorGreen,

    ColorWrite

} Color ;


//动物性别的枚举值

typedef enum {

    SexBoy,

    SexGirle,

    SexUnknown

} Sex ;


//声明Animal类的属性

@interface Animal : NSObject

{

    Color _color ;

    Sex _sex ;

    int _age ;

}


//设置动物的颜色

- (void)setColor:(Color)color ;

//获得动物的颜色

- (Color)color ;


//设置动物的性别

- (void)setSex:(Sex)sex ;

//获得动物的性别

- (Sex)sex ;


//设置动物的年龄

- (void)setAge:(int)age ;

//获得动物年龄

- (int)age ;


//动物跑的行为

- (void)run ;

//动物吃的行为

- (void)feed ;


@end


//实现Animal的功能

@implementation Animal

- (void)setColor:(Color)color

{

    _color = color ;

}


- (Color)color

{

    return _color ;

}


- (void)setSex:(Sex)sex

{

    _sex = sex ;

}


- (Sex)sex

{

    return _sex ;

}


- (void)setAge:(int)age

{

    _age = age ;

}


- (int)age

{

    return _age ;

}


- (void)run

{

    NSLog(@"动物,-----跑起来了");

}


- (void)feed

{

    NSLog(@"动物,-----吃饱了");

}


@end


@interface Dog : Animal

- (void)run ;

- (void)feed ;

+ (void)run ;

+ (void)feed ;

@end


@implementation Dog

- (void)run

{

    NSLog(@"狗狗,-----跑起来了");

}

- (void)feed

{

    NSLog(@"狗狗,-----吃饱了");

}

+ (void)run

{

    NSLog(@"狗,++++++跑起来了");

}

+ (void)feed

{

    NSLog(@"狗,++++++吃饱了");

}

@end


int main()

{

    Animal * animal = [Animal new] ;

    [animal run] ;

    //Animal * a1 = [Dog new];

    //[Dog run];可以这样转换但是有缺陷,不建议使用

    

    //使用强制转化法 父类指针转化为子类对象

    Animal * a1 = [Dog new];

    Dog * p1 = (Dog *)a1

    [p1 run];

    [Dog run];

    [a1 run] ;

    return 0 ;

}



























































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值