A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

#import <Foundation/Foundation.h>

enum{GOODMAN,BADMAN} MAN;

@interface Animal : NSObject
{
    int _legCount;
    int _eyeCount;
}
-(void)setLegCount:(int)legCount;
-(int)getLegCount;
-(void)setEyeCount:(int)eyeCount;
-(int)getEyeCount;

-(void)run;
-(void)eat;
@end

@implementation Animal

-(void)setLegCount:(int)legCount{
    _legCount = legCount;
}
-(int)getLegCount{
    return _legCount;
}
-(void)setEyeCount:(int)eyeCount{
    _eyeCount = eyeCount;
}
-(int)getEyeCount{
    return _eyeCount;
}

-(void)run{
    NSLog(@"runing...");
}
-(void)eat{
    NSLog(@"eating...");
}
@end

@interface Dog : Animal
-(void)watchHouse;
-(void)biteMan;
@end

@implementation Dog
-(void)watchHouse{
   
    switch (MAN) {
        case GOODMAN:
            NSLog(@"Watching House...");
            break;
        case BADMAN:
            [self biteMan];
            break;
        default:
            break;
    }
}
-(void)biteMan{
    NSLog(@"Bite a man...");
}
@end

@interface Cat : Animal
-(void)climbTree;
@end

@implementation Cat
-(void)climbTree{
    [self eat];
    NSLog(@"Climbing Tree...");
}
@end
int main(){
    Dog *d = [Dog new];
    Cat *c = [Cat new];
    MAN=BADMAN;
    [d watchHouse];
    [c climbTree];

}

2 个回复

倒序浏览
个人觉得-(void)watchHouse这个方法加个参数会更好
回复 使用道具 举报
发达哥绝对是个人才
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马