黑马程序员技术交流社区
标题:
继承--类与类关联-super&&self
[打印本页]
作者:
YuePr
时间:
2015-12-7 21:23
标题:
继承--类与类关联-super&&self
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
char *_name;
}
-(void)run;
-(void)catchThief;
@end
@implementation Dog
-(void)run
{
NSLog(@"%s在跑",_name);
}
-(void)catchThief
{
NSLog(@"警犬抓小偷");
}
@end
@interface Police : NSObject
{
Dog *_dog;
char *_name;
}
-(void)run;
+(void)catchThief;
-(void)setDog:(Dog *)dog;
-(Dog *)getDog;
@end
@implementation Police
-(void)run
{
NSLog(@"%s在跑",_name);
}
+(void)catchThief
{
NSLog(@"警察抓小偷");
}
-(void)setDog:(Dog *)dog
{
_dog = dog;
}
-(Dog *)getDog
{
return _dog;
}
@end
int main()
{
Police *jinPing = [Police new];
Dog *dog = [Dog new];
[jinPing setDog:dog];
[[jinPing getDog] catchThief];
return 0;
}
复制代码
作者:
连桦懿
时间:
2015-12-7 21:31
写的不错啊
作者:
649685603
时间:
2015-12-7 21:40
不明所以
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2