Objective-C题目及解答(一)

题目要求:
                1. 使用@property自动生成成员变量的setter和getter方法。
                2. 尽量使用点语法。
                3. 在.h中尽量使用@class,在.m中使用#import
                4. 尽量使用构造方法对实例对象进行初始化。
                5. 体会类的本质,类名就是类对象。
                6. 用description方法来实现打印实例对象的各成员变量。
A类题
a. 1 设计三个类,动物类、鸟类、狗类,类之间关系自拟。

1. Animal
(1)属性
* 年龄、毛色、身长
(2) 方法
三个自定义的构造方法:无参数的(设置属性为固定值,年龄为1岁,毛色为黑色,身长为1cm),有两个参数的(可同时初始化年龄和毛色),三个参数的(可同时初始化年龄、毛色和身长)。

description方法:依次打印出年龄,毛色,身长。


Animal.h

#import 

typedef enum {
    black,
    white,
    yellow,
    red,
    green
} HairColor;

@interface Animal : NSObject
{
    NSInteger _age;
    HairColor _haircolor;
    NSInteger _height;
}

@property NSInteger age,height;
@property HairColor haircolor;

- (id)initWithAge:(NSInteger)age andHaircolor:(HairColor)haircolor;
- (id)initWithAge:(NSInteger)age andHaircolor:(HairColor)haircolor andHeight:(NSInteger)height;
@end


Animal.m
#import "Animal.h"

@implementation Animal
- (instancetype)init
{
    if (self = [super init])
    {
        _age = 1;
        _haircolor = black;
        _height = 1;
    }
    return self;
}

- (id)initWithAge:(NSInteger)age andHaircolor:(HairColor)haircolor
{
    if (self = [super init])
    {
        _age = age;
        _haircolor = haircolor;
    }
    return self;
}

- (id)initWithAge:(NSInteger)age andHaircolor:(HairColor)haircolor andHeight:(NSInteger)height
{
    if (self = [self initWithAge:age andHaircolor:haircolor])
    {
        _height = height;
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"\nage = %li\nhaircolor = %i\nheight = %li",_age,_haircolor,_height];
}
@end

2. Bird
(1)属性
*年龄、毛色、身长、飞行速度
(2)方法
一个自定义的构造方法:可以同时初始化年龄、毛色、身长、飞行速度。

description方法:依次打印出年龄,毛色,身长、飞行速度。


Bird.h

#import "Animal.h"

@interface Bird : Animal
{
    NSInteger _flySpeed;
}
@property NSInteger flySpeed;

- (id)initWithAge:(NSInteger)age andHaircolor:(HairColor)haircolor andHeight:(NSInteger)height andFlySpeed:(NSInteger)flySpeed;
@end

Bird.m

#import "Bird.h"

@implementation Bird

- (id)initWithAge:(NSInteger)age andHaircolor:(HairColor)haircolor andHeight:(NSInteger)height andFlySpeed:(NSInteger)flySpeed
{
    if (self = [super initWithAge:age andHaircolor:haircolor andHeight:height])
    {
        _flySpeed = flySpeed;
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"\nage = %li\nhaircolor = %i\nheight = %li\nflySpeed = %li",_age,_haircolor,_height,_flySpeed];
}
@end

3. Dog
(1)属性

*年龄、毛色、身长、奔跑最大距离

(2)方法

一个自定义的构造方法:可以同时初始化年龄、毛色、身长,奔跑最大距离。
description方法:依次打印出年龄,毛色,身长,奔跑最大距离

实现:跑  捡球  


Dog.h

#import "Animal.h"

@interface Dog : Animal
{
    NSInteger _runMaxDistance;
}
@property NSInteger runMaxDistance;
- (id)initWithAge:(NSInteger)age andHaircolor:(HairColor)haircolor andHeight:(NSInteger)height andRunMaxDistance:(NSInteger)runMaxDistance;

- (void)run;
- (void)pickBall;
- (void)bark;
@end

Dog.m

#import "Dog.h"

@implementation Dog
- (id)initWithAge:(NSInteger)age andHaircolor:(HairColor)haircolor andHeight:(NSInteger)height andRunMaxDistance:(NSInteger)runMaxDistance
{
    if (self = [super initWithAge:age andHaircolor:haircolor andHeight:height])
    {
        _runMaxDistance = runMaxDistance;
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"\nage = %li\nhaircolor = %i\nheight = %li\nrunMaxDistace = %li",_age,_haircolor,_height,_runMaxDistance];
}

- (void)run
{
    NSLog(@"狗在跑");
}

- (void)pickBall
{
    NSLog(@"狗玩捡球的游戏");
}

- (void)bark
{
    NSLog(@"逗狗叫");
}
@end

a.2设计一个类,人类
1.Person
(1)属性
* 姓名,身高,拥有一只宠物狗
(2)方法
* 属性相应的set和get方法
* 设计一个自定义构造方法初始化人的姓名,身高,所拥有狗的年龄、毛色、身长、奔跑最大距离。
* description方法:依次打印出此人的姓名,身高,其拥有宠物狗的年龄,毛色,身长,奔跑最大距离。

一个遛狗的方法,可以传入一个时间值,9点带狗出去跑,10点和狗玩捡球的游戏,11点逗狗叫。


Person.h

#import 
@class Dog;

@interface Person : NSObject
{
    NSString* _name;
    NSInteger _height;
    Dog* _petDog;
}
@property NSString* name;
@property NSInteger height;
@property Dog* petDog;

- (id)initWithName:(NSString*)name andHeight:(NSInteger)height andDog:(Dog*)petDog;
- (void)walkTheDog:(NSInteger)time;
@end

Person.m

#import "Person.h"
#import "Dog.h"

@implementation Person
- (id)initWithName:(NSString *)name andHeight:(NSInteger)height andDog:(Dog *)petDog
{
    if (self = [super init])
    {
        _name= name;
        _height = height;
        _petDog = petDog;
    }
    return self;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"人:\nname = %@\nheight = %li\n狗:\nage = %li\nhaircolor = %i\nheight = %li\nrunMaxDistace = %li",_name,_height,[_petDog age],[_petDog haircolor],[_petDog height],[_petDog runMaxDistance]];
}

- (void)walkTheDog:(NSInteger)time
{
    NSLog(@"%li点带狗出去跑,%li点和狗玩捡球的游戏,%li点逗狗叫",time,time+1,time+2);
}
@end

A类题测试代码

#import 
#import "Animal.h"
#import "Person.h"
#import "Dog.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Animal* animal = [[Animal alloc] initWithAge:3 andHaircolor:black andHeight:38];
        NSLog(@"%@",animal);
        Dog* dog = [[Dog alloc] initWithAge:2 andHaircolor:white andHeight:66 andRunMaxDistance:100];
        Person* p = [[Person alloc] initWithName:@"lilei" andHeight:180 andDog:dog];
        
        NSLog(@"%@",p);
        [p walkTheDog:9];
        }
        return 0;
}


B类题
b.1 设计2个类,复数类和分数类

1.Complex
(1)属性
* 实部,虚部
(2)方法
* 属性相应的set和get方法
* 重写description打印相应的实部虚部值

* 设计一个对象方法获取当前实例对象与其它实例对象相加以后的结果(实部相加,虚部相加)


 Complex.h

#import 

@interface Complex : NSObject
{
    NSInteger _real;
    NSInteger _imaginary;
}
@property NSInteger real;
@property NSInteger imaginary;

- (Complex*)addWithOther:(Complex*)other;
@end


Complex.m
#import "Complex.h"

@implementation Complex

- (NSString *)description
{
    return [NSString stringWithFormat:@"real part:%li\nimaginary part:%li",_real,_imaginary];
}

- (Complex *)addWithOther:(Complex *)other
{
    Complex* result = [[Complex alloc] init];
    result.real = self.real + other.real;
    result.imaginary = self.imaginary + other.imaginary;
    return result;
}
@end

2. Fraction
(1)属性
* 分子, 分母
(2)方法
* 属性相应的set和get方法
* 设计一个对象方法获取当前实例对象与其它实例对象相加以后的结果(两个分数相加的结果)
3. 假设两个类的处理相加操作的方法名相同 addWithOther :
4. 在main函数中定义两个Complex实例对象myComplex1, myComplex2,两个Fraction实例对象myFract1,myFract2,思考:下列操作是否合理:
result = [myComplex1 addWithOther: myComplex2];
NSLog(@”%@”,result);
result = [myFract1 addWtihOther:myFract2];
NSLog(@”%@”,result);

可否用同一种数据类型的变量保存两个操作的结果?result应该为哪种类型?


Fraction.h

#import 

@interface Fraction : NSObject
{
    NSInteger _numerator;
    NSInteger _denominator;
}
@property NSInteger numerator;
@property NSInteger denominator;

- (Fraction*)addWithOther:(Fraction*)f;
//简化分数
- (void)reduce
 
  

 
  

Fraction.m

#import "Fraction.h"

@implementation Fraction

- (Fraction *)addWithOther:(Fraction *)f
{
    Fraction* fnew = [[Fraction alloc] init];
    fnew.numerator = self.numerator * f.denominator + self.denominator * f.numerator;
    fnew.denominator = self.denominator * f.denominator;
    return fnew;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"numerator part:%li\ndenominator part:%li",_numerator,_denominator];
}

- (void)reduce
{
    NSInteger u = self.numerator;
    NSInteger v = self.denominator;
    NSInteger temp;
    
    while (v)
    {
        temp = u % v;
        u = v;
        v = temp;
    }
    self.numerator /= u;
    self.denominator /= u;
}
@end


B类题测试代码
#import 
#import "Complex.h"
#import "Fraction.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        Complex* myComplex1 = [[Complex alloc] init];
        Complex* myComplex2 = [[Complex alloc] init];
        Fraction* myFract1 = [[Fraction alloc] init];
        Fraction* myFract2 = [[Fraction alloc] init];
        
        myComplex1.real = 2;
        myComplex2.real = 3;
        myComplex1.imaginary = 4;
        myComplex2.imaginary = 5;
        myFract1.numerator = 1;
        myFract1.denominator = 4;
        myFract2.numerator = 1;
        myFract2.denominator = 2;
        
        id result = [myComplex1 addWithOther: myComplex2];
        NSLog(@"%@",result);
        result = [myFract1 addWithOther:myFract2];
        [result reduce];
        NSLog(@"%@",result);
    }
    return 0;
}

C类题
c.1 设计一个学生类。

(1)属性
* 姓名,学号(用字符串表示)
(2)方法
* 设计一个setter方法setName:来设置此学生的姓名。
* 设计一个setter方法setName:andNum:来同时设置学生的姓名和学号。
* 设计一个私有方法setAnonymousName:andNum:来将学生姓名设置为匿名,将学生学号设置为1000。
*设计一个方法可以调用上述私有方法。
(3)selector
* 使用SEL类型来包装上述方法。
* 通过选择器来实现上述方法的调用。

* 假设有字符串对象@”setName:andNum:”,如何将此对象转化成SEL类型,再去调用上述第二个方法。


Student.h

#import 

@interface Student : NSObject
{
    NSString* _name;
    NSString* _num;
}
@property NSString* name;
@property NSString* num;

//- (void)setName:(NSString *)name;
- (void)setName:(NSString *)name andNum:(NSString *)num;
- (void)testPrivate;
@end


Student.m
#import "Student.h"

@implementation Student

//- (void)setName:(NSString *)name
//{
//    _name = name;
//}

- (void)setName:(NSString *)name andNum:(NSString *)num
{
    _name = name;
    _num = num;
}

- (void)setAnonymousNameandNum
{
    _name = nil;
    _num = @"1000";
}

- (void)testPrivate
{
    NSLog(@"这里先调用了testPrivate方法");
    [self setAnonymousNameandNum];
}
@end


C类题测试代码
#import 
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student* s = [[Student alloc] init];
        SEL s1 = @selector(setName:);
        SEL s2 = @selector(setName:andNum:);
        SEL s3 = @selector(setAnonymousNameandNum);
        SEL s4 = @selector(testPrivate);
        
        [s performSelector:s1 withObject:@"lilei1"];
        NSLog(@"%@",[s name]);
        [s performSelector:s2 withObject:@"lilei2" withObject:@"99999"];
        NSLog(@"%@  %@",s.name,s.num);
        [s performSelector:s3];
        NSLog(@"%@  %@",s.name,s.num);
        [s performSelector:s4];
        NSLog(@"%@  %@",s.name,s.num);
        
        //只知道方法名,而方法名是一个字符串,可以把字符串对象转换为一个SEL类型的数据
        NSString* sa = @"setName:andNum:";
        SEL s5 = NSSelectorFromString(sa);
        [s performSelector:s5 withObject:@"lilei3" withObject:@"66666"];
        NSLog(@"%@  %@",s.name,s.num);
    }
    return 0;
}

你可能感兴趣的:(objective-c,类,class)