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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Flower. 中级黑马   /  2015-11-29 13:55  /  706 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*继承练习

设计一个警察抓小偷的场景,二者都有身高,体重,姓名三个属性和都有跑的方法.
警察有枪的属性和抓小偷的方法.
小偷有小刀的属性和偷东西的方法.

*/
#import <Foundation/Foundation.h>
//父类
@interface Person : NSObject{
    char *_name;//姓名
    double _high;//身高
    double _weight;//体重
}
-(void)run;
-(void)setName:(char *)name;

@end

@implementation Person

-(void)run{
   
    NSLog(@"%s 跑....",_name);
}

-(void)setName:(char *)name{
    _name=name;
}

@end

//警察
@interface Police : Person{
   
    char *_qiang;
}
+(void)catchThief;
@end

@implementation Police


+(void)catchThief{
    NSLog(@"抓小偷...");
}

@end


//小偷
@interface Thief : Person{
    char *_dao;
}
+(void)tou;
@end

@implementation Thief

+(void)tou{
     NSLog(@"偷东西...");
}

@end

int main(){
    //小偷偷东西, 警察跑去抓小偷,小偷跑路
   Thief *t= [Thief new];
    [t setName:"thief"];
    Police *p=[Police new];
    [p setName:"police"];
   
    [Thief tou];
    [p run];
    [Police catchThief];
    [t run];
   
   
   
   
    return 0;
}

2 个回复

倒序浏览
谢谢分享。。。
回复 使用道具 举报
哈哈哈哈哈哈哈
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马