Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

继承、多态、选择器、单例、匿名类别

 

 

 .h--->.m---->.h--->.m---->.m

———————————————————————分割——————————————————————————————

_父类指针指向子类对象

#import <Cocoa/Cocoa.h>

 

@interface Animals : NSObject

 

@property (nonatomic)NSInteger age;

@property (nonatomic, copy)NSString *name;

 

- (void)printAge:(NSInteger)newAge andName:(NSString *)newName;

 

@end

 

 

#import "Animals.h"

 

@implementation Animals

 

- (void)printAge:(NSInteger)newAge andName:(NSString *)newName

{

    NSLog(@"className = %@", [self class]);

    NSLog(@"name = %@ age = %lu",newName, newAge);

}

 

@end

 

#import "Animals.h"

 

@interface Cat : Animals

 

// (void)printAge:(NSInteger)newAge andName:(NSString *)newName;

@end

 

 

#import "Cat.h"

 

@implementation Cat

 

- (void)printAge:(NSInteger)newAge andName:(NSString *)newName

{

    NSLog(@"当前类为%@类", [self class]);

    NSLog(@"cat name is %@, age is %lu", newName,newAge);

}

@end

 

 

 

#import "Animals.h"

 

@interface Dog : Animals

 

//- (void)printAge:(NSInteger)newAge andName:(NSString *)newName;

@end

 

 

 

#import "Dog.h"

 

@implementation Dog

 

- (void)printAge:(NSInteger)newAge andName:(NSString *)newName

{

    NSLog(@"当前类为%@类",[self class]);

    NSLog(@"dog name is %@, age is %lu", newName, newAge);

}

 

@end

 

#import <Foundation/Foundation.h>

#import "Animals.h"

#import "Dog.h"

#import "Cat.h"

 

void printAnimalsInfomation(Animals *ani, NSInteger age, NSString *name)

{

    [ani printAge:age andName:name];

}

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

//        Animals *ani = [[Animals alloc] init];

//        [ani printAge:12 andName:@"未知"];

//        

//        Dog *dog = [[Dog alloc] init];

//        ani = dog;

//        [ani printAge:14 andName:@"小黑"];

//        

//        Cat *cat = [[Cat alloc] init];

//        ani = cat ;

//        [ani printAge:15 andName:@"咪咪"];

        //多态的一种

        Dog *dog1 = [[Dog alloc] init];

        printAnimalsInfomation(dog1,12, @"小黑");

        Cat *cat1 = [[Cat alloc] init];

        printAnimalsInfomation(cat1, 14, @"小花");

        

    }

    return 0;

}

 

 

 

 .h--->.m---->.h--->.m---->.m

———————————————————————分割——————————————————————————————

 

#import <Foundation/Foundation.h>

 

@interface Person : NSObject

{

    //默认缺省是@protected

    NSInteger num;

    

    @protected//保护的

    NSInteger _age;

    NSString *_name;

    

    @private//私有的

    NSInteger _ID;

    BOOL _sex;

    

    @public//公共的

    float _height;

    float _weight;

}

 

 

- (void)setAge:(NSInteger)newAge;

- (void)setName:(NSString *)newName;

 

//- (void)setID:(NSInteger)newID;

- (void)setSex:(BOOL)newSex;

 

- (void)setHeight:(float)newHeight;

- (void)setWeight:(float)newWeight;

 

@property (readonly)NSInteger age;

 

@property (readwrite)  NSInteger ID;

 

@property (readwrite) NSString *place;//私有的

 

@end

 

 

#import "Person.h"

 

@implementation Person

 

- (void)setAge:(NSInteger)newAge

{

   self -> _age = newAge;//省略了self 

}

 

- (void)setName:(NSString *)newName

{

    _name = newName;

}

 

- (void)setSex:(BOOL)newSex

{

    _sex = newSex;

}

 

//- (void)setID:(NSInteger)newID

//{

//    _ID = newID;

//}

 

- (void)setWeight:(float)newWeight

{

    _weight = newWeight;

}

 

- (void)setHeight:(float)newHeight

{

    _height = newHeight;

}

@end

 

 

 

#import "Person.h"

 

@interface Student : Person

 

- (void)printAgeAndName;

 

- (void)changePlace;

 

@end

 

 

#import "Student.h"

 

@implementation Student

 

 

- (void)printAgeAndName

{

    NSLog(@"name is %@, age is %li", _name, _age);

}

 

- (void)changePlace

{

    //place = @"zhongguo";

}

@end

 

 

#import "Person.h"

 

@interface Teacher : Person

 

- (void)printIDAndSex;

 

- (void)printHeightAndWeight;

 

@end

 

 

#import "Teacher.h"

 

@implementation Teacher

 

 

- (void)printIDAndSex

{

   //_ID = 456789;//子类不能继承私有成员变量

    num = 12;

    

}

 

- (void)printHeightAndWeight

{

    _weight = 456;

    _height = 678;

}

@end

 

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Student.h"

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        /*

         //@protected

       Person *xiaoHua = [[Person alloc] init];

       //xiaoHua->_name= @"xiaoHua";//保护的实例变量在当前类内可以直接访问,在类外不能直接访问,但是可以通过方法调用间接访问,子类可以直接继承保护的实例变量,子类可以直接访问保护的实例变量

        [xiaoHua setAge:12];

        NSLog(@"xiaoHua age is %li", xiaoHua.age);

       Student *stu = [[Student alloc] init];

        [stu setAge:20];

        [stu setName:@"xiaoZhang"];

        [stu printAgeAndName];

        */

        /*

         //@private

       Person *xiaoMing =  [[Person alloc] init];

        //xiaoMing ->_ID;//私有的成员变量,在当前类内可以直接访问, 在类外不能直接访问,可以通过方法调用间接访问,子类不能继承私有成员变量

        //[xiaoMing setID:1234567890];

        xiaoMing.ID = 2345678;

        NSLog(@"xiaoMing id is %lu", xiaoMing.ID);

        */

        //@public

        Person *xiaoHua = [[Person alloc] init];

        xiaoHua->_height= 178.8;//公有的实例变量在类内类外都能直接访问,子类可以直接继承, 也能直接访问

        xiaoHua->_weight = 67.5;

        

    }

    return 0;

}

 

 

 

 .h--->.m---->.h--->.m---->.m

———————————————————————分割——————————————————————————————

多态

#import <Foundation/Foundation.h>

 

@interface Animal : NSObject

 

- (void)showAnimalsFood;

 

@end

 

 

#import "Animal.h"

 

@implementation Animal

 

-(void)showAnimalsFood

{

    return;

}

@end

 

 

#import "Animal.h"

 

@interface Cat : Animal

 

@end

 

 

#import "Cat.h"

 

@implementation Cat

 

- (void)showAnimalsFood

{

    NSLog(@"猫爱吃鱼");

}

 

@end

 

 

#import "Animal.h"

 

@interface Dog : Animal

 

@end

 

 

#import "Dog.h"

 

@implementation Dog

 

- (void)showAnimalsFood

{

    NSLog(@"狗喜欢吃骨头");

}

 

@end

 

 

#import "Animal.h"

 

@interface Frog : Animal

 

@end

 

 

 

#import "Frog.h"

 

@implementation Frog

 

- (void)showAnimalsFood

{

    NSLog(@"青蛙爱吃虫子");

}

@end

 

 

#import <Foundation/Foundation.h>

#import "Animal.h"

 

@interface Hander : NSObject

 

- (void)printAnimalFoodAction:(Animal *)ani;

 

@end

 

 

#import "Hander.h"

 

@implementation Hander

 

- (void)printAnimalFoodAction:(Animal *)ani

{

    [ani showAnimalsFood];

}

 @end

 

 

#import "Animal.h"

@interface Snake : Animal

@end

 

 

#import "Snake.h"

 

@implementation Snake

 

- (void)showAnimalsFood

{

    NSLog(@"蛇喜欢吃老鼠");

}

 

@end

 

 

#import <Foundation/Foundation.h>

#import "Hander.h"

#import "Snake.h"

#import "Frog.h"

#import "Cat.h"

#import "Dog.h"

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Hander *xiaoHong = [[Hander alloc] init];

        Snake *snake = [[Snake alloc] init];

        [xiaoHong printAnimalFoodAction:snake];

        Frog *frog = [[Frog alloc] init];

        [xiaoHong printAnimalFoodAction:frog];

        Dog *dog = [[Dog alloc] init];

        [xiaoHong printAnimalFoodAction:dog];

        Cat *cat = [[Cat alloc] init];

        [xiaoHong printAnimalFoodAction:cat];  

    }

    return 0;

}

 

 

 

 .h--->.m---->.h--->.m---->.m

———————————————————————分割——————————————————————————————

#import <Foundation/Foundation.h>

 

@interface NSMutableString (Reverse)

 

- (NSString *)reverseString;

 

@end

 

 

#import "NSMutableString+Reverse.h"

 

@implementation NSMutableString (Reverse)

 

- (NSString *)reverseString

{

    NSMutableString *mulStr = [NSMutableString string];

    NSInteger len = [self length];

    for (NSInteger i=len-1; i>=0; i--) {

        [mulStr appendFormat:@"%C", [self characterAtIndex:i]];

    }

    [self setString:mulStr];

    return  self;

}

 

@end

 

 

#import <Foundation/Foundation.h>

 

@interface NSString (Print)//Print类别名

 

- (void)print;

 

@end

 

 

#import "NSString+Print.h"

 

@implementation NSString (Print)

 

- (void)print

{

    NSLog(@"self = %@", self);

}

 

@end

 

 

#import <Foundation/Foundation.h>

#import "NSString+Print.h"

#import "NSMutableString+Reverse.h"

 

//Category

//给类添加方法(常用给系统类添加方法)

//若要使用类别中的方法,需要添加对应的头文件

//类别主要用来给类扩展方法

//类别不能给类扩展实例变量

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

       NSString *str = @"hello world";

        [str print];

        NSMutableString *mulStr = [NSMutableString stringWithFormat:@"我是中国人"];

        [mulStr reverseString];

        NSLog(@"str = %@", mulStr);

        

    }

    return 0;

}

 

 .h--->.m---->.h--->.m---->.m

———————————————————————分割——————————————————————————————

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

 

@property (nonatomic)NSInteger age;

@property (copy,readwrite)NSString *name;

 

- (void)setID:(NSInteger)newID;

 

- (NSInteger)ID;

 

- (void)printDog;

 

@end

 

 

#import "Dog.h"

@interface Dog ()

{

    NSInteger _ID;//匿名类别可以给类扩展实例变量,实例是私有的,子类不能继承

}

 

//@property (nonatomic)NSInteger ID;

 

- (void)printInformation;//私有方法

 

 

@end

 

 

@implementation Dog

 

- (void)setID:(NSInteger)newID

{

    _ID = newID;

}

 

- (NSInteger)ID

{

    return _ID;

}

 

- (void)printInformation

{

    NSLog(@"name is %@, age is %lu,ID is %lu", self.name, self.age , self.ID);

}

 

- (void)printDog//公开

{

    [self printInformation];

}

 

 

@end

 

 

#import <Foundation/Foundation.h>

#import "Dog.h"

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Dog *dog = [[Dog alloc] init];

        dog.age = 12;

        dog.name = @"xiaoHei";

        dog.ID = 14;

        //[dog printInformation];

        [dog printDog];

        

    }

    return 0;

}

 

 

 

 

 .h--->.m---->.h--->.m---->.m

———————————————————————分割——————————————————————————————

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

 

@property (nonatomic)NSInteger age;

@property (nonatomic,copy,readwrite)NSString *name;

 

- (void)print;

 

- (void)printName:(NSString *)name;

 

- (void)printName:(NSString *)name andAge:(NSNumber *)age;

 

@end

 

 

#import "Dog.h"

 

@implementation Dog

 

- (void)print

{

    NSLog(@"name is %@, age is %lu", self.name, self.age);

}

 

- (void)printName:(NSString *)name

{

    NSLog(@"name = %@", name);

}

 

- (void)printName:(NSString *)name andAge:(NSNumber *)age

{

    NSLog(@"name = %@ age = %@", name , age);

}

 

 

@end

 

 

#import <Foundation/Foundation.h>

#import "Dog.h"

 

//选择器

 

//函数指针

int add(int a, int b)

{

    return a+b;

}

 

//

 

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        int (*padd)(int,int) = add;

        NSLog(@"add = %d", padd(3,5));

        NSLog(@"add = %p", padd);//利用函数的地址调用函数

#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

        SEL sel = @selector(print);//运行时概念

        Dog *dog = [[Dog alloc] init];

        dog.name = @"xiaoHei";

        dog.age = 13;

        if ([dog respondsToSelector:sel]) {//判断选择器中的方法是否实现

            NSLog(@"%p", sel);

            [dog performSelector:sel];//执行选择器中保存的方法

        }

        else{

            NSLog(@"该方法在当前类中没有实现");

        }

        if([dog respondsToSelector:@selector(printName:)])

        {

            [dog performSelector:@selector(printName:) withObject:@"xiaoHei"];

        }

        else

        {

            NSLog(@"该方法在当前类中没有实现");

        }

        

        SEL sel2 = NSSelectorFromString(@"printName:andAge:");//利用方法名字符串,创建选择器

        if ([dog respondsToSelector:sel2]) {

            [dog performSelector:sel2 withObject:@"qianfeng" withObject:[NSNumber numberWithInteger:12]];

            

        }

        else{

            NSLog(@"该方法在当前类中没有实现");

        }

        

        SEL sel3 = sel_getUid("print");//利用C语言字符串创建选择器

        if ([dog respondsToSelector:sel3]) {

            [dog performSelector:sel3];

        }

        else

        {

            

        }

    }

    return 0;

}

 

 

 

 .h--->.m---->.h--->.m---->.m

———————————————————————分割——————————————————————————————

#import <Foundation/Foundation.h>

 

@interface MusicManager : NSObject

{

    NSString *_name;

}

 

+ (MusicManager *)defaultMusicManager;

 

@property (copy)NSString *name;

 

@end

 

 

#import "MusicManager.h"

 

static MusicManager *music;

 

@implementation MusicManager

 

//创建单例对象

//方法一

//+ (MusicManager *)defaultMusicManager

//{

//    if(!music)

//    {

//        music = [[self alloc] init];

//    }

//    return music;

//}

 

//+ (MusicManager *)defaultMusicManager

//{

//    @synchronized(self)//加线程锁,防止多线程访问造成内存泄露

//    {

//        if (!music) {

//            music = [[self alloc] init];

//        }

//    }

//    return music;

//}

 

//GCD

+ (MusicManager *)defaultMusicManager

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        if (!music) {

            music = [[MusicManager alloc] init];

        }

    });

    return music;

}

 

- (NSString *)description

{

    NSString *str = [NSString stringWithFormat:@"%@", self.name];

    return str;

}

 

 

@end

 

 

 

#import <Foundation/Foundation.h>

#import "MusicManager.h"

 

//单例类

//单例对象在内存中只有一份

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        MusicManager *mu1 = [MusicManager defaultMusicManager];

        mu1.name = @"QQ音乐";

        NSLog(@"%p : %@", mu1,mu1);

        MusicManager *mu2 = [MusicManager defaultMusicManager];

        NSLog(@"%p : %@", mu2,mu2);

         

    }

    return 0;

}

 

 

 

 

 .h--->.m---->.h--->.m---->.m

———————————————————————分割——————————————————————————————

#import <Foundation/Foundation.h>

 

@interface Father : NSObject

 

@property (assign, nonatomic)NSString *name;

@property (assign, nonatomic, readwrite)NSInteger age;

- (id)initWithName:(NSString *)newName andAge:(NSInteger)newAge;

 

- (void)eat;

 

@end

 

 

 

#import "Father.h"

 

@implementation Father

 

- (id)initWithName:(NSString *)newName andAge:(NSInteger)newAge

{

    self = [super init];

    if (self) {

        _name = newName;

        _age = newAge;

    }

    return  self;

}

 

- (void)eat

{

    NSLog(@"喜欢吃水果");

}

 

@end

 

 

 

#import "Father.h"

 

@interface Son : Father

 

@property (assign,readwrite) int number;

 

 

- (void)action;

 

@end

 

 

#import "Son.h"

 

@implementation Son

 

- (void)action

{

    NSLog(@"%@去钓鱼",[self class]);//class 获取类名

}

 

- (void)eat//重写父类方法

{

    NSLog(@"喜欢吃蔬菜");

}

 

- (NSString *)description//重写打印方法

{

    NSString *str = [NSString stringWithFormat:@"name = %@, age = %li",self.name, self.age];

    

    return str;

}

 

@end

 

 

 

#import <Foundation/Foundation.h>

#import "Son.h"

#import "Father.h"

 

//封装 继承 多态

 

//继承

//

//father   son

 

//父类             子类

//fatherClass   sonClass subClass childClass

//superClass

 

//子类继承父类

//实例变量: 继承过来的实例变量加自定义的实例变量

//方法: 父类方法加子类方法

//子类可以重写父类的方法

 

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Son *xiaoHua = [[Son alloc] init];

        xiaoHua.age = 20;

        xiaoHua.name = @"小华";

        NSLog(@"name = %@, age = %li", xiaoHua.name, xiaoHua.age);

        [xiaoHua action];

        [xiaoHua eat];//先从当前类,查找对应的方法, 如果没有,跳到父类中查找对应的方法

        NSLog(@"%@",xiaoHua);//自动调用discription方法

        Father *person =xiaoHua;//父类的指针指向子类的对象

        [person eat];//父类指针指向子类对象,调用方法取决于对象本身,先从对象本身对应的类查找方法,找不到,再到父类中查找方法

    }

    return 0;

}

 

 

posted on 2014-03-05 19:55  Sinner_Yun  阅读(293)  评论(0编辑  收藏  举报

导航