Do you have a class information program that contains 4 defined classes: Ourclass, Teacher, Student, person, and implementation methods.

Source: Internet
Author: User

    1. #import "Teacher.h"
    2. @interface Ourclass:nsobject
    3. @property (nonatomic, retain) Teacher *tea;
    4. @property (nonatomic, retain) Nsmutablearray *arr;
    5. @end </span>


  1. #import "OurClass.h"
  2. @implementation Ourclass
  3. -(NSString *) description
  4. {
  5. return [NSString stringwithformat:@"%@", _arr];
  6. }
  7. -(void) Dealloc
  8. {
  9. NSLog (@"class space release");
  10. [_tea release];
  11. [_arr release];
  12. [Super Dealloc];
  13. }
  14. @end
  15. </span>

< param name= "wmode" value= "Transparent" >
    1. @interface Person:nsobject
    2. {
    3. NSString *_name;
    4. Nsinteger _age;
    5. }
    6. @property (nonatomic, retain) NSString *name;
    7. @property (nonatomic) Nsinteger age;
    8. @end </span>

< param name= "wmode" value= "Transparent" >
    1. <span style="FONT-SIZE:18PX;" > @interface Teacher:person
    2. -(NSNumber *) exam: (NSString *) course;
    3. @end </span>


< param name= "wmode" value= "Transparent" >
    1. <span style= "FONT-SIZE:18PX;" > @implementation  teacher  
    2. -  (nsnumber *) exam: (nsstring *) course  
    3. {&NBSP;&NBSP;
    4.     nsnumber *score  = [nsnumber numberwithfloat:arc4random ()  % 101];  
    5.     return score;  
    6. }  
    7. -  (void) dealloc  
    8. {  
    9.     nslog (@
    10.     [_name release];  
    11.      [super dealloc];  
    12. }  
    13. @end </span >  


    1. <span style="FONT-SIZE:18PX;" > @interface Student:person
    2. @property (nonatomic, retain) nsmutabledictionary *score;
    3. Initialization
    4. -(ID) Initwithname: (NSString *) Name: (Nsinteger) Age Score: (Nsmutabledictionary *) score;
    5. Compare Age Size
    6. -(Nscomparisonresult) Comparebyage: (Student *) anstudent;
    7. @end </span>


  1. @implementation Student
  2. -(ID) Initwithname: (NSString *) Name: (Nsinteger) Age Score: (Nsmutabledictionary *) score
  3. {
  4. self = [super init];
  5. if (self) {
  6. Self.name = name;
  7. Self.age = age;
  8. Self.score = score;
  9. }
  10. return self;
  11. }
  12. -(Nscomparisonresult) Comparebyage: (Student *) anstudent
  13. {
  14. if ([self] > [anstudent]) {
  15. return nsordereddescending;
  16. }Else if ([self-age] = = [Anstudent age]) {
  17. return nsorderedsame;
  18. }Else {
  19. return nsorderedascending;
  20. }
  21. }
  22. -(NSString *) description
  23. {
  24. return [NSString stringwithformat:@"%@,%ld,%@", _name,_age,_score];
  25. }
  26. -(void) Dealloc
  27. {
  28. NSLog (@"*******");
  29. [_name release];
  30. [_score release];
  31. [Super Dealloc];
  32. }
  33. @end
  34. </span>


  1. #import "Student.h"
  2. #import "OurClass.h"
  3. int main (int argc, const char * argv[])
  4. {
  5. @autoreleasepool {
  6. Nsmutabledictionary *dic1 = [nsmutabledictionary dictionarywithobjectsandkeys:@87,@"Yuwen", nil];
  7. Nsmutabledictionary *dic2 = [nsmutabledictionary dictionarywithobjectsandkeys:@94,@"Yuwen", nil];
  8. Nsmutabledictionary *dic3 = [nsmutabledictionary dictionarywithobjectsandkeys:@76,@"Yuwen", nil];
  9. Student *STU1 = [[Student alloc] initwithname:@"xiaoming" age:23 Score:dic1]; //0-1
  10. Student *STU2 = [[Student alloc] initwithname:@"Xiaohong" age:35 Score:dic2]; //0-1
  11. Student *STU3 = [[Student alloc] initwithname:@"Xiaofang" age:17 score:dic3]; //0-1
  12. Nsmutablearray *stuarr = [[Nsmutablearray alloc] initwithobjects:stu1,stu2, nil]; //0-1
  13. [STU1 release];
  14. [STU2 release];
  15. Ourclass *class = [[Ourclass alloc] init]; //0-1
  16. Class.arr = Stuarr; //1-2
  17. [Stuarr release]; //2-1
  18. NSLog (@"%@",Class);
  19. //change of teacher
  20. Teacher *teacher = [[Teacher alloc] init]; //0-1
  21. Class.tea = teacher; //1-2
  22. [Teacher release]; //2-1
  23. //Add student
  24. [Class.arr ADDOBJECT:STU3];
  25. NSLog (@"%@",Class);
  26. [STU3 release];
  27. //Remove Students
  28. [Class.arr REMOVEOBJECT:STU3];
  29. NSLog (@"%@",Class);
  30. //Student Age comparison
  31. Nscomparisonresult result = [STU1 COMPAREBYAGE:STU2];
  32. NSLog (@"%ld", result);
  33. //Set Student scores
  34. NSNumber *num = [teacher exam:@"Yuwen"];
  35. NSLog (@"score is%@", num);
  36. [class release];
  37. }
  38. return 0;
  39. }
  40. </span>

Do the class information program, which contains 4? defined classes: Ourclass, Teacher, Student, person, and implementation methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.