- #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;
- }
复制代码 |