实现班级信息程序

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic) NSInteger age;

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;
+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age;

@end
#import "Person.h"

@implementation Person

- (void)dealloc
{
[_name release];
[super dealloc];
}

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {
if (self = [super init]) {
self.name = name;
self.age = age;
}
return self;
}

+ (instancetype)personWithName:(NSString *)name age:(NSInteger)age {
return [[[Person alloc] initWithName:name age:age] autorelease];
}

- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@, age:%ld", _name, _age];
}

@end
#import "Person.h"

@class OurClass;

@interface Teacher : Person

@property (nonatomic, assign) OurClass *ourClass;

- (void)exam:(NSString *)subject;

@end

#import "Teacher.h"
#import "OurClass.h"
#import "Student.h"

@implementation Teacher

- (void)exam:(NSString *)subject {
for (Student *stu in _ourClass.studentArray) {
stu.resultDic[subject] = [NSString stringWithFormat:@"%u", arc4random() % 101];
NSLog(@"%@: %@", stu.name, stu.resultDic[subject]);
}

}

@end
#import "Person.h"

@interface Student : Person

@property (nonatomic) NSInteger number;
@property (nonatomic, retain) NSMutableDictionary *resultDic;

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age number:(NSInteger)number;

- (BOOL)compareAgeWithStudent:(Student *)stu;
+ (BOOL)compareAgeStudent:(Student *)aStu withAnotherStudent:(Student *)bStu;

@end
#import "Student.h"

@implementation Student

- (void)dealloc
{
[_resultDic release];
[super dealloc];
}

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age number:(NSInteger)number {
self = [super initWithName:name age:age];
if (self) {
self.number = number;
self.resultDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0", @"Chinese", @"0", @"Math", @"0", @"English", nil];
}
return self;
}

- (BOOL)compareAgeWithStudent:(Student *)stu {
return self.age > stu.age;
}

+ (BOOL)compareAgeStudent:(Student *)aStu withAnotherStudent:(Student *)bStu {
return aStu.age > bStu.age;
}

- (NSString *)description
{
return [NSString stringWithFormat:@"%@ number:%ld, resultDic:%@", [super description], _number, _resultDic];
}

@end
#import <Foundation/Foundation.h>

@class Teacher, Student;

@interface OurClass : NSObject

@property (nonatomic, retain) Teacher *teacher;
@property (nonatomic, retain) NSMutableArray *studentArray;

- (instancetype)initWithTeacher:(Teacher *)teacher;

- (void)addStudent:(Student *)stu;
- (void)removeStudent:(Student *)stu;
- (void)removeStudentWithNumber:(NSInteger)number;

@end
#import "OurClass.h"
#import "Teacher.h"
#import "Student.h"

@implementation OurClass

- (void)dealloc
{
[_teacher release];
[_studentArray release];
[super dealloc];
}

- (instancetype)initWithTeacher:(Teacher *)teacher {
if (self = [super init]) {
self.teacher = teacher;
self.studentArray = [NSMutableArray arrayWithCapacity:0];
}
return self;
}

- (void)addStudent:(Student *)stu {
[_studentArray addObject:stu];
}

- (void)removeStudent:(Student *)stu {
[_studentArray removeObject:stu];
}

- (void)removeStudentWithNumber:(NSInteger)number {
for (NSInteger i = 0; i < _studentArray.count; i++) {
Student *stu = _studentArray[i];
if (stu.number == number) {
[_studentArray removeObjectAtIndex:i];
break;
}
}
}

@end
#import <Foundation/Foundation.h>
#import "Teacher.h"
#import "Student.h"
#import "OurClass.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {
//实现班级信息程序
//1、定义3个类:OurClass、Teacher、Student。
//2、Teacher类,属性:姓名、年龄、班级,方法:考试(exam:)。
//3、Student类,属性:姓名、年龄、学号、成绩,⽅法:比较年龄。
//4、OurClass类,属性:老师、学生,方法:添加学生和移除学生。
Teacher *teacher = [[Teacher alloc] initWithName:@"辉哥" age:18];

Student *stu1 = [[Student alloc] initWithName:@"小明" age:30 number:1024];
Student *stu2 = [[Student alloc] initWithName:@"朱岩" age:5 number:1007];
Student *stu3 = [[Student alloc] initWithName:@"程磊" age:2 number:1234];

OurClass *ourClass = [[OurClass alloc] initWithTeacher:teacher];

[ourClass addStudent:stu1];
[ourClass addStudent:stu2];
[ourClass addStudent:stu3];

[stu1 release];
[stu2 release];
[stu3 release];

teacher.ourClass = ourClass;
[teacher exam:@"English"];

[teacher release];
[ourClass release];

}
return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值