Xcode-------main.m:
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//Dog *dog = [[Dog alloc]initWithName:@"二哈" WithColor:@"red"];
Person *pp = [[Person alloc]initWithName:@"小明"];
int time;
NSLog(@"请输入时间:");
scanf("%d",&time);
[pp walkDog:time];
}
return 0;
}
Xcode-------Person.h:
#import <Foundation/Foundation.h>
#import "Dog.h"
@interface Person : NSObject
{
NSString *_name;
Dog *_dog;
}
- (id)initWithName:(NSString *)name;
- (void)walkDog:(int)time;
@end
Xcode-------Person.m:
#import "Person.h"
#import "Dog.h"
@implementation Person
- (id)initWithName:(NSString *)name{
self = [super init];
if (self != nil) {
_name = name;
_dog = [[Dog alloc]initWithName:@"小白" WithColor:@"白色"];
}
return self;
}
- (void)walkDog:(int)time{
if (time == 9) {
[_dog run];
}
else if (time == 10){
[_dog pickupBall];
}
else if (time == 11) {
[_dog bark];
}
else{
[_dog sleep];
}
}
@end
Xcode-------Dog.h:
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
NSString *_name;
NSString *_color;
}
//自定义初始化方法
- (id)initWithName:(NSString *)name WithColor:(NSString *)color;
- (void)run;
- (void)pickupBall;
- (void)bark;
- (void)sleep;
@end
Xcode--------Dog.m:
#import "Dog.h"
@implementation Dog
- (id)initWithName:(NSString *)name WithColor:(NSString *)color{
if (self != nil) {
self = [super init];
_name = name;
_color = color;
}
return self;
}
- (void)run{
NSLog(@"奔跑吧,小白!");
}
- (void)pickupBall{
NSLog(@"飞奔捡球~");
}
- (void)bark{
NSLog(@"旺旺!");
}
- (void)sleep{
NSLog(@"zzz~zz~");
}
@end