zoukankan      html  css  js  c++  java
  • NSObject的常见方法

    代码:

    #import <Foundation/Foundation.h>
    
    /******************************
     * Learning协议
     ******************************/
    @protocol Learning
    
    - (void)learn;
    
    @end
    
    /******************************
     * Person类
     ******************************/
    @interface Person : NSObject
    @end
    
    @implementation Person
    @end
    
    /******************************
     * Student类
     ******************************/
    @interface Student : Person <Learning>
    @end
    
    @implementation Student
    
    - (void)learn {
        NSLog(@"Student - learn");
    }
    
    @end
    
    /******************************
     * GoodStudent类
     ******************************/
    @interface GoodStudent : Student
    @end
    
    @implementation GoodStudent
    
    - (void)learn {
        NSLog(@"GoodStudent - learning");
    }
    @end
    
    void isKindOfClassTest(void);
    void isMemberOfClassTest(void);
    void conformsToProtocolTest(void);
    void instanceResopnsesToSelectorTest(void);
    void respondsToSelectorTest(void);
    
    /******************************
     * main函数
     ******************************/
    int main(int argc, const char* argv[]) {
        isKindOfClassTest();
        isMemberOfClassTest();
        conformsToProtocolTest();
        instanceResopnsesToSelectorTest();
        respondsToSelectorTest();
        return 0;
    }
    
    void isKindOfClassTest(void) {
        Student* student = [[Student alloc] init];
    
        // 实例方法isKindOfClass:用于判断一个实例是否是某个类或其子类的实例
        NSLog([student isKindOfClass:[Person class]] ? @"YES" : @"NO");
        NSLog([student isKindOfClass:[Student class]] ? @"YES" : @"NO");
        NSLog([student isKindOfClass:[GoodStudent class]] ? @"YES" : @"NO");
    }
    
    void isMemberOfClassTest(void) {
        Student* student = [[Student alloc] init];
    
        // 实例方法isMemberOfClass:用于判断一个实例是否是某个类实例
        NSLog([student isMemberOfClass:[Person class]] ? @"YES" : @"NO");
        NSLog([student isMemberOfClass:[Student class]] ? @"YES" : @"NO");
        NSLog([student isMemberOfClass:[GoodStudent class]] ? @"YES" : @"NO");
    }
    
    void conformsToProtocolTest(void) {
        Person* person = [[Person alloc] init];
        Student* student = [[Student alloc] init];
        GoodStudent* goodStudent = [[GoodStudent alloc] init];
    
        // 实例方法conformsToProtocol:用于判断一个类是否采用了某个协议
        NSLog([person conformsToProtocol:@protocol(Learning)] ? @"YES" : @"NO");
        NSLog([student conformsToProtocol:@protocol(Learning)] ? @"YES" : @"NO");
        NSLog([goodStudent conformsToProtocol:@protocol(Learning)] ? @"YES" : @"NO");
    }
    
    void instanceResopnsesToSelectorTest(void) {
        // 类方法instancesRespondToSelector:用于判断一个类的实例是否可以响应给定的消息
        NSLog([Person instancesRespondToSelector:@selector(learn)] ? @"YES" : @"NO");
        NSLog([Student instancesRespondToSelector:@selector(learn)] ? @"YES" : @"NO");
        NSLog([GoodStudent instancesRespondToSelector:@selector(learn)] ? @"YES" : @"NO");
    }
    
    void respondsToSelectorTest(void) {
        Person* person = [[Person alloc] init];
        Student* student = [[Student alloc] init];
        GoodStudent* goodStudent = [[GoodStudent alloc] init];
    
        // 实例方法respondsToSelector:用于判断某个类的实例是否可以响应给定的消息
        NSLog([person respondsToSelector:@selector(learn)] ? @"YES" : @"NO");
        NSLog([student respondsToSelector:@selector(learn)] ? @"YES" : @"NO");
        NSLog([goodStudent respondsToSelector:@selector(learn)] ? @"YES" : @"NO");
    }

    输出:

    YES
    YES
    NO
    NO
    YES
    NO
    NO
    YES
    YES
    NO
    YES
    YES
    NO
    YES
    YES
  • 相关阅读:
    C#序列化和反序列化开发者在线 Builder.com.cn 更新时间:20080904
    提高C#编程水平不可不读的50个要诀 开发者在线 Builder.com.cn 更新时间:20080805作者: 来源:开发者在线
    CollapsiblePanelExtender这Ajax控件
    100个冷笑话,越往后越冷(郁闷时专用……)赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞赞
    [转]eclipse java build path
    [转]线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
    水晶报表最后空页解决方法
    JQuery资源
    Windows Explorer中对所选文件增加右键菜单并关联自己程序的例子
    [转]Oracle如何复制表的sql语句
  • 原文地址:https://www.cnblogs.com/xwoder/p/4252245.html
Copyright © 2011-2022 走看看