#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;
}