zoukankan      html  css  js  c++  java
  • student.m main.m

    #import <Foundation/Foundation.h>
    #import "Student.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Student *stu1=[[Student alloc]initWithName:@"wang" andGender:@"male" andAge:@28];
            Student *stu2=[[Student alloc]initWithName:@"yang" andGender:@"female" andAge:@27];
            Student *stu3=[[Student alloc]initWithName:@"lang" andGender:@"male" andAge:@26];
            NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:stu1.name,@"name",stu1.gender,@"gender",stu1.age,@"age", nil];
            NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:stu2.name,@"name",stu2.gender,@"gender",stu2.age,@"age", nil];
            NSDictionary *dic3=[NSDictionary dictionaryWithObjectsAndKeys:stu3.name,@"name",stu3.gender,@"gender",stu3.age,@"age", nil];
            NSArray *arr=[NSArray arrayWithObjects:dic1,dic2,dic3, nil];
            [Student displayFirstAndLastName:arr];
            [Student caculateFirstAndLastAge:arr];
        }
        return 0;
    }

    .h

    #import <Foundation/Foundation.h>
    
    @interface Student : NSObject
    -(instancetype)initWithName:(NSString *)newName andGender:(NSString *)newGender andAge:(NSNumber *)newAge;
    +(void)displayFirstAndLastName:(NSArray *)array;
    +(void)caculateFirstAndLastAge:(NSArray *)array;
    @property NSString *name;
    @property NSString *gender;
    @property NSNumber *age;
    @end

    .m

    #import "Student.h"
    
    @implementation Student
    -(instancetype)initWithName:(NSString *)newName andGender:(NSString *)newGender andAge:(NSNumber *)newAge
    {
        name=newName;
        gender=newGender;
        age=newAge;
        return self;
    }
    +(void)displayFirstAndLastName:(NSArray *)array
    {
        NSDictionary *dicFirst=[array firstObject];
        NSDictionary *dicLast=[array lastObject];
        NSLog(@"first name:%@,last name:%@",dicFirst[@"name"],dicLast[@"name"]);
    }
    +(void)caculateFirstAndLastAge:(NSArray *)array
    {
        NSDictionary *dicFirst=[array firstObject];
        NSDictionary *dicLast=[array lastObject];
        int firstAge=[dicFirst[@"age"] intValue];
        int lastAge=[dicLast[@"age"] intValue];
        NSLog(@"Age between first and last:%d",abs(firstAge-lastAge));
    }
    @synthesize name,gender,age;
    @end
  • 相关阅读:
    83. Remove Duplicates from Sorted List
    141. Linked List Cycle
    hdu1028 划分数
    XDU1019 阶乘因子的个数
    poj2773 容斥原理
    poj1091 容斥原理的应用
    poj1173 多重集组合数
    HDU 1465 错排问题
    poj 1496
    复习之求一个数的约束之积模一个质数
  • 原文地址:https://www.cnblogs.com/hezhuangzhuang/p/5123241.html
Copyright © 2011-2022 走看看