做⼀个班级信息程序,包含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>
相关文章推荐
- 做⼀个班级信息程序,包含4个⾃定义的类:OurClass、Teacher、 Student、Person,并实现方法.
- 实现简单通讯录操作。 1、定义联系人类AddressPerson。实例变量:姓名、性别、电话号码、住址、分组名称。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息 2、在main.m中定义可变
- 3、写一个程序,定义一个学生结构体,至少要含4个数据项,其中有一项用来保存学生成绩。用此结构体存储班级上的所有学生信息
- 在一个程序中需要用到全局变量(在多个class之间共享数据),请问如何定义具有这种功能的变量?或者是否有其他的方法解决多个class之间的数据共享(尽量简单实现)。 首先应该明确 Java中没有全局变
- 用C语言定义Bool类型(包括数据表示和操作实现),至 少包含and(与)、or(或)、not(非)、equiv(相等)这4个操作。
- 对于spring实现bean信息定义目标-基于三种方法的思考
- Java实例 定义任务的一个发射小程序 实现Runnable接口并编写run()方法
- C#实现将程序运行信息写入日志的方法
- JavaScript 实现命名空间(namespace)的最佳方案——兼容主流的定义类(class)的方法,兼容所有浏览器,支持用JSDuck生成文档
- 定义一个类DOG,包含age,weight等属性,以及对这些属性操作的方法,实现并测试这个类。
- 2、按以下要求编写程序 (1) 编写Animal接口,接口中声明run() 方法 (2) 定义Bird类和Fish类实现Animal接口 (3) 编写Bird类和Fish类的测试程序,并调用其
- 笔试题:创建一个单链表,结点包含学生的学号,姓名,性别,年龄信息.写几个程序,实现按学生学号插入,查询,删除等操作.
- //实现中等难度通讯录。需求: //1、定义联系人类Contact。实例变量:姓名(拼音,首字母大写)、性别、电话号码、住址、分组名称、年龄。方法:自定义初始化方法(姓名、电话号码)、显示联系人信息
- 创建Employee类,在类中定义三个属性:编号,姓名,年龄,然后在构造方法里初始化这三个属性,最后载实现接口中的定义的CompareTo方法,将对象按编号升序排列。根据已有的代码,补全程序。
- 走进C++程序世界-------类的定义和使用(数据成员和方法成员,析构函数,构造函数,内联实现)
- 使用HashMap编写一程序实现存储某班级学生信息
- jQuery 选择同时包含两个class的元素的实现方法
- 用C语言定义Bool类型(包括数据表示和操作实现),至 少包含and(与)、or(或)、not(非)、equiv(相等)这4个操作。
- VC++实现通过API来查看程序错误信息的方法
- 使用ShellClass获取文件属性详细信息的实现方法