2022年iOS实习期实践作业

2022年iOS实习期实践作业

[toc]

第一周

练习实践1:子类重写父类的方法

#import <Foundation/Foundation.h>

//声明类Student
@interface Student : NSObject  //通过@interface关键字来声明类

//类所包含的变量
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, assign) double score;

//类所包含的函数
- (void)display;

@end  //使用@end关键字结束类的声明

//实现在类中声明的函数
@implementation Student  //通过@implementation关键字来实现类中的函数

- (void)display {
    NSLog(@"%@的年龄是 %d,成绩是 %f", self.name, self.age, self.score);
}

@end  //使用@end关键字结束类中函数的实现


//声明动物类
@interface Animal : NSObject

- (void)run;

- (void)eat;

@end

//为动物类添加方法
@implementation Animal

- (void)run {
    NSLog(@"动物都会移动");
}

- (void)eat{
    NSLog(@"动物都要吃东西");
}

@end

//声明猫和狗
@interface Cat : Animal

@end

@interface Dog : Animal

@end

//重写父类方法
@implementation Cat

- (void)run {
    NSLog(@"猫跑可以的很快");
}

- (void)eat {
    NSLog(@"猫喜欢吃鱼");
}

@end

@implementation Dog

- (void)run {
    NSLog(@"狗也跑可以的很快");
}

- (void)eat {
    NSLog(@"狗喜欢吃骨头");
}

@end

int main() {
    Cat *cat = [[Cat alloc] init];
    Dog *dog = [[Dog alloc] init];
    [cat run];
    [cat eat];
    [dog run];
    [dog eat];
    return 0;
}

练习实践2:两数之和

1. 两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:

输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:

输入:nums = [3,3], target = 6
输出:[0,1]

C++ 解法:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int len = nums.size();
        unordered_map<int, int> map;

        for(int i = 0; i < len; i++) {
            auto iterator = map.find(target - nums[i]);
            if(iterator != map.end()) return {iterator->second, i};
            map[nums[i]] = i;
        }
        return {};
    }
};

Objective-C解法:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSMutableArray *inputArray = [NSMutableArray arrayWithCapacity:0];
        int target;
        int len;
        
        NSLog(@"请输入数组长度");
        scanf("%d", &len);
        NSLog(@"请输入数组");
        
        for(int i = 0; i < len; i++) {
            int tmp;
            scanf("%d", &tmp);
            [inputArray addObject:@(tmp)];
        }
        
        NSLog(@"请输入两数之和");
        scanf("%d", &target);
        
        NSMutableDictionary *valueToIndexDict = [NSMutableDictionary dictionary];

        NSInteger tmpValue;
        NSNumber *findIndex;
        
        for(int i = 0; i < len; i++) {
            tmpValue = [inputArray[i] integerValue];
            findIndex = @(target - tmpValue);
            
            if(valueToIndexDict[findIndex] != nil) {
                NSLog(@"%@ %d", valueToIndexDict[findIndex], i);
            }
            [valueToIndexDict setValue:@(i) forKey:(inputArray[i])];
        }
    }
    return 0;
}

练习实践3:重写类的 init 函数

注意命名规范,重写的 init 函数要以 init 开头

#import <Foundation/Foundation.h>

//声明动物类
@interface Animal : NSObject

@property (nonatomic, assign) int height;
@property (nonatomic, assign) int weight;
@property (nonatomic, assign) int age;
@property (nonatomic, copy) NSString *name;

- (void)run;

- (void)eat;

- (id)initWithHeight:(int)height weight:(int)weight age:(int)age name:(NSString *)name;

@end

//为动物类添加方法
@implementation Animal

- (void)run {
    NSLog(@"动物都会移动");
}

- (void)eat{
    NSLog(@"动物都要吃东西");
}

- (void)printAnimalData {
    NSLog(@"它的各项数据是:身高:%d 体重:%d 年龄:%d 名称:%@",self.height, self.weight, self.age, self.name);
}

- (id)initWithHeight:(int)height weight:(int)weight age:(int)age name:(NSString *)name{
    if(self = [super init]) {
        self.height = height;
        self.weight = weight;
        self.age = age;
        self.name = name;
    }
    return self;
}

@end

//声明猫和狗
@interface Cat : Animal

@end

@interface Dog : Animal

@end

//重写父类方法
@implementation Cat

- (void)run {
    NSLog(@"猫跑可以的很快");
}

- (void)eat {
    NSLog(@"猫喜欢吃鱼");
}

@end

@implementation Dog

- (void)run {
    NSLog(@"狗也跑可以的很快");
}

- (void)eat {
    NSLog(@"狗喜欢吃骨头");
}

@end

int main() {
    Cat *cat = [[Cat alloc] initWithHeight:11 weight:33 age:44 name:@"bey"];
    Dog *dog = [[Dog alloc] initWithHeight:2 weight:49 age:488 name:@"huang"];
    [cat printAnimalData];
    [cat run];
    [cat eat];
    [dog printAnimalData];
    [dog run];
    [dog eat];
    return 0;
}
------------- 本文结束 感谢阅读 -------------