/*继承练习
设计一个警察抓小偷的场景,二者都有身高,体重,姓名三个属性和都有跑的方法.
警察有枪的属性和抓小偷的方法.
小偷有小刀的属性和偷东西的方法.
*/
#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;
} |
|