#import <Foundation/Foundation.h>
//父类--动物
@interface Animal : NSObject{//声明
char *_name;
}
-(void)setName:(char *)name;
-(char *)getName;
-(void)eat;
@end
@implementation Animal //实现
-(void)setName:(char *)name{
_name=name;
}
-(char *)getName{
return _name;
}
-(void)eat{
NSLog(@"动物吃东西...");
}
@end
//动物类的子类-狗
@interface Dog : Animal
-(void)eat;
-(void)kanMen;//狗独有的方法
@end
@implementation Dog
-(void)eat{
NSLog(@"狗吃东西...");
}
-(void)kanMen{
NSLog(@"狗看门...");
}
@end
//动物类的子类-猫
@interface Cat : Animal
-(void)eat;
@end
@implementation Cat
-(void)eat{
NSLog(@"小猫吃东西...");
}
@end
//动物类的子类-猪
@interface Pig : Animal
-(void)eat;
@end
@implementation Pig
-(void)eat{
NSLog(@"小猪吃东西...");
}
@end
//多态
void eatfood(Animal *am);
void eatfood(Animal *am){
for (int i=0; i<3; i++) {
[am eat];
}
}
int main(){
return 0;
} |
|