您的位置:首页 > 其它

做⼀个班级信息程序,包含4个⾃定义的类:OurClass、Teacher、 Student、Person,并实现方法.

2014-08-17 16:49 483 查看
#import "Teacher.h"
@interface OurClass : NSObject
@property (nonatomic, retain) Teacher *tea;
@property (nonatomic, retain) NSMutableArray *arr;
@end</span>


#import "OurClass.h"
@implementation OurClass
- (NSString *)description
{
return [NSString stringWithFormat:@"%@",_arr];
}
- (void)dealloc
{
NSLog(@"班级空间释放");
[_tea release];
[_arr release];
[super dealloc];
}
@end
</span>
@interface Person : NSObject
{
NSString *_name;
NSInteger _age;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic) NSInteger age;
@end</span>


<span style="font-size:18px;">@interface Teacher : Person
- (NSNumber *)exam:(NSString *)course;
@end</span>


<span style="font-size:18px;">@implementation Teacher
- (NSNumber *)exam:(NSString *)course
{
NSNumber *score = [NSNumber numberWithFloat:arc4random() % 101];
return score;
}
- (void)dealloc
{
NSLog(@"老师空间释放");
[_name release];
[super dealloc];
}
@end</span>


<span style="font-size:18px;">@interface Student : Person
@property (nonatomic, retain) NSMutableDictionary *score;
//初始化
- (id)initWithName:(NSString *)name age:(NSInteger)age score:(NSMutableDictionary *)score;
//比较年龄大小
- (NSComparisonResult)compareByAge:(Student *)anStudent;
@end</span>


@implementation Student
- (id)initWithName:(NSString *)name age:(NSInteger)age score:(NSMutableDictionary *)score
{
self = [super init];
if (self) {
self.name = name;
self.age = age;
self.score = score;
}
return self;
}
- (NSComparisonResult)compareByAge:(Student *)anStudent
{
if ([self age] > [anStudent age]) {
return NSOrderedDescending;
}else if([self age] == [anStudent age]){
return NSOrderedSame;
}else {
return NSOrderedAscending;
}
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@,%ld,%@",_name,_age,_score];
}
- (void)dealloc
{
NSLog(@"*******");
[_name release];
[_score release];
[super dealloc];
}
@end
</span>


#import "Student.h"
#import "OurClass.h"
int main(int argc, const char * argv[])
{

@autoreleasepool {
NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@87,@"yuwen", nil];
NSMutableDictionary *dic2 = [NSMutableDictionary   dictionaryWithObjectsAndKeys:@94,@"yuwen", nil];
NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@76,@"yuwen", nil];
Student *stu1 = [[Student alloc] initWithName:@"xiaoming" age:23 score:dic1];   // 0 -1
Student *stu2 = [[Student alloc] initWithName:@"xiaohong" age:35 score:dic2];  // 0 -1
Student *stu3 = [[Student alloc] initWithName:@"xiaofang" age:17 score:dic3];   //0 - 1
NSMutableArray *stuArr = [[NSMutableArray alloc] initWithObjects:stu1,stu2, nil];    // 0 -1
[stu1 release];
[stu2 release];
OurClass *class = [[OurClass alloc] init];   // 0 - 1
class.arr = stuArr;  // 1 - 2
[stuArr release];// 2 - 1
NSLog(@"%@",class);
//更换老师
Teacher *teacher = [[Teacher alloc] init];  // 0 - 1
class.tea = teacher;   // 1 - 2
[teacher release];     // 2 - 1
//添加学生
[class.arr addObject:stu3];
NSLog(@"%@",class);
[stu3 release];
//移除学生
[class.arr removeObject:stu3];
NSLog(@"%@",class);
//学生年龄比较
NSComparisonResult result = [stu1 compareByAge:stu2];
NSLog(@"%ld",result);
//设置学生成绩
NSNumber *num = [teacher exam:@"yuwen"];
NSLog(@"分数为%@",num);
[class release];
}
return 0;
}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐