OC类的继承和重写

main.m

//
//  main.m
//  12.19
//
//  Created by lanqs on 14/12/19.
//  Copyright (c) 2014年 . All rights reserved.
//

#import <Foundation/Foundation.h>
#import "animals.h"
#import "animal.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        /*
         
         编写动物世界中的继承树
         动物类具有身高,体重,颜色等特征,进食,行走等动作
         编写食肉动物和食草动物两个类,继承自动物类,重写父类的方法
         编写老虎,狮子等食肉动物类,找出应该继承的父类,并且重写父类的方法
         编写牛羊等食草动物,找出应该继承的父类,并且重写父类的方法
         
         */
        [animal action];
        [eatMeat action];
        [eatgrass action];
        [tiger action];
        [lion action];
        [cow action];
        [sheep action];
  
    }
    return 0;
}


animal.h

//
//  animal.h
//  12.19
//
//  Created by lanqs on 14/12/19.
//  Copyright (c) 2014年 . All rights reserved.
//

#import <Foundation/Foundation.h>

@interface animal : NSObject{
    CGFloat _height;
    CGFloat _weight;
    NSString *_color;
}

+ (void)action;
@end

animal.m

//
//  animal.m
//  12.19
//
//  Created by lanqs on 14/12/19.
//  Copyright (c) 2014年 . All rights reserved.
//

#import "animal.h"

@implementation animal
+ (void)action{
    NSLog(@"进食,行走");
}
@end
animals.h

//
//  animals.h
//  12.19
//
//  Created by lanqs on 14/12/19.
//  Copyright (c) 2014年 . All rights reserved.
//

#import <Foundation/Foundation.h>
#import "animal.h"
@interface animals : NSObject

@end
@interface eatMeat : animal

@end
@interface eatgrass : animal

@end
@interface tiger : eatMeat

@end
@interface lion : eatMeat

@end
@interface cow : eatgrass

@end@interface sheep : eatgrass

@end

animals.m
//
//  animals.m
//  12.19
//
//  Created by lanqs on 14/12/19.
//  Copyright (c) 2014年 . All rights reserved.
//

#import "animals.h"

@implementation animals

@end
//
@implementation eatMeat
+(void)action{
    NSLog(@"吃肉动物");
}
@end
@implementation eatgrass
+(void)action{
    NSLog(@"吃肉动物");
}
@end
@implementation tiger
+(void)action{
    NSLog(@"老虎");
}
@end
@implementation lion
+(void)action{
    NSLog(@"狮子");
}
@end
@implementation cow
+(void)action{
    NSLog(@"牛");
}
@end
@implementation sheep
+(void)action{
    NSLog(@"羊");
}
@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值