要求: 当学习到第三次或者第五次的时候,就用电脑玩游戏放松一下;
设计一个学生类Student;
有一个学习的方法:study
有一个学习的次数:time
有一个电脑类:Computer
电脑有个玩游戏的方法playGames;
//main.h
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Computer.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
//NSLog(@"Hello, World!");
Student *stu1=[Student new];
//Computer *cmp=[Computer new];
[stu1 study];
[stu1 study];
[stu1 study];
[stu1 study];
[stu1 study];
}
return 0;
}
//Computer.h
#import <Foundation/Foundation.h>
@interface Computer : NSObject
//玩游戏声明
+(void)playGames;
@end
//Computer.m
#import "Computer.h"
@implementation Computer
//玩游戏实现(私有方法)
+(void)playGames{
NSLog(@"玩游戏");
}
@end
//Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
//学习声明
-(void)study;
@end
//Student.m
#import "Computer.h"
#import "Student.h"
@implementation Student
//私有变量
{
int _times;
}
//学习实现
-(void)study{
_times++;
switch (_times) {
case 3:
NSLog(@"正在学习,已学习3次");
//NSLog(@"2!!");
//[stu1 Computer];
[Computer playGames];
break;
case 5:
NSLog(@"正在学习,已学习5次");
//NSLog(@"2!!");
[Computer playGames];
break;
default:
NSLog(@"正在学习!!");
break;
}
}
@end
|
|