//Stundent.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
int _time;
}
-(void)setStudyTime:(int)time;
@end
//Student.m
#import "Student.h"
#import "Computer.h"
@implementation Student
{
int i;
}
-(void)setStudyTime:(int)time{
_time = time;
for(i = 1 ;i <= _time;i++){
if(i == 3 || i == 5){
Computer *computer = [Computer new];
[computer playComputer];
}else{
[self study];
}
}
}
-(void)study{
NSLog(@"学生正在学习");
}
@end
//Computer.h
#import <Foundation/Foundation.h>
@interface Computer : NSObject
-(void)playComputer;
@end
//Computer.m
#import "Computer.h"
#import "Student.h"
@implementation Computer
-(void)playComputer{
NSLog(@"学习累了该去玩会电脑了!");
}
@end
//main.m
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Computer.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *stu = [Student new];
[stu setStudyTime:7];
}
return 0;
}
这个还可以再简化一点或者换一种别的方法么? |