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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值