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
#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
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
*年龄、毛色、身长、奔跑最大距离
(2)方法
一个自定义的构造方法:可以同时初始化年龄、毛色、身长,奔跑最大距离。实现:跑 捡球 叫
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
#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
一个遛狗的方法,可以传入一个时间值,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
#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
#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;
}
* 设计一个对象方法获取当前实例对象与其它实例对象相加以后的结果(实部相加,虚部相加)
Complex.h
#import
@interface Complex : NSObject
{
NSInteger _real;
NSInteger _imaginary;
}
@property NSInteger real;
@property NSInteger imaginary;
- (Complex*)addWithOther:(Complex*)other;
@end
#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
可否用同一种数据类型的变量保存两个操作的结果?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
#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;
}
* 假设有字符串对象@”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
#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
#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;
}