-
做⼀个班级信息程序,包含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];
- Student *stu2 = [[Student alloc] initWithName:@"xiaohong" age:35 score:dic2];
- Student *stu3 = [[Student alloc] initWithName:@"xiaofang" age:17 score:dic3];
- NSMutableArray *stuArr = [[NSMutableArray alloc] initWithObjects:stu1,stu2, nil];
- [stu1 release];
- [stu2 release];
- OurClass *class = [[OurClass alloc] init];
- class.arr = stuArr;
- [stuArr release];
- NSLog(@"%@",class);
-
- Teacher *teacher = [[Teacher alloc] init];
- class.tea = teacher;
- [teacher release];
-
- [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>
-
相关阅读:
团体程序设计天梯赛PTA L1-006连续因子
团体程序设计天梯赛PTA L1-002打印沙漏
spring学习3-配置文件
markdown基本用法
java贪食蛇小游戏
在idea中使用lombook插件
ajax初体验hello_ajax
idea,自定义骨架的增加与删除
idea 2017,2018,2019下载与破解
idea关联mysql数据库失败,时区错误,数据库驱动配置
-
原文地址:https://www.cnblogs.com/as5346/p/4476919.html
Copyright © 2020-2023
润新知