- #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 (@"class space release");
- [_tea release];
- [_arr release];
- [Super Dealloc];
- }
- @end
- </span>
< param name= "wmode" value= "Transparent" >
- @interface Person:nsobject
- {
- NSString *_name;
- Nsinteger _age;
- }
- @property (nonatomic, retain) NSString *name;
- @property (nonatomic) Nsinteger age;
- @end </span>
< param name= "wmode" value= "Transparent" >
- <span style="FONT-SIZE:18PX;" > @interface Teacher:person
- -(NSNumber *) exam: (NSString *) course;
- @end </span>
< param name= "wmode" value= "Transparent" >
- <span style= "FONT-SIZE:18PX;" > @implementation teacher
- - (nsnumber *) exam: (nsstring *) course
- {&NBSP;&NBSP;
- 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;
- Initialization
- -(ID) Initwithname: (NSString *) Name: (Nsinteger) Age Score: (Nsmutabledictionary *) score;
- Compare Age Size
- -(Nscomparisonresult) Comparebyage: (Student *) anstudent;
- @end </span>
- @implementation Student
- -(ID) Initwithname: (NSString *) Name: (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] > [anstudent]) {
- 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);
- //change of teacher
- Teacher *teacher = [[Teacher alloc] init]; //0-1
- Class.tea = teacher; //1-2
- [Teacher release]; //2-1
- //Add student
- [Class.arr ADDOBJECT:STU3];
- NSLog (@"%@",Class);
- [STU3 release];
- //Remove Students
- [Class.arr REMOVEOBJECT:STU3];
- NSLog (@"%@",Class);
- //Student Age comparison
- Nscomparisonresult result = [STU1 COMPAREBYAGE:STU2];
- NSLog (@"%ld", result);
- //Set Student scores
- NSNumber *num = [teacher exam:@"Yuwen"];
- NSLog (@"score is%@", num);
- [class release];
- }
- return 0;
- }
- </span>
Do the class information program, which contains 4? defined classes: Ourclass, Teacher, Student, person, and implementation methods.