• Objective-C:Foundation框架-常用类-NSObject


    NSObject是所有类的基类,其常见用法有: 

    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    - (void)test;
    @end
    
    #import "Person.h"
    
    @implementation Person
    - (void)test {
        NSLog(@"调用了Person的test方法");
    }
    @end
    
    #import "Person.h"
    
    @interface Student : Person
    
    - (void)test;
    
    - (void)test2:(NSString *)a;
    
    @end
    
    #import "Student.h"
    
    @implementation Student
    
    - (void)test {
        NSLog(@"调用了test方法");
    }
    
    - (void)test2:(NSString *)a {
        NSLog(@"调用了test2方法:%@", a);
    }
    
    @end
    #import <Foundation/Foundation.h>
    #import "Student.h"
    #import "Person.h"
    
    #pragma mark NSObject常用方法
    void objUse() {
        Student *stu = [[[Student alloc] init] autorelease];
        
        // isKindOfClass判断对象是否属于某个类 或者 子类
        if ( [stu isKindOfClass:[Person class]] ) {
            // NSLog(@"stu属于Person或者继承自Person");
        }
        
        // isMemberOfClass判断对象是否属于某个类(不包括子类)
        BOOL result = [stu isMemberOfClass:[Student class]];
        // NSLog(@"%i", result);
        
        
        // 直接调用
        // [stu test];
        
        // 间接调用
        [stu performSelector:@selector(test)];
        // [stu performSelector:@selector(test2:) withObject:@"abc"];
        // 延迟2秒后调用test2:方法
        [stu performSelector:@selector(test2:) withObject:@"abc" afterDelay:2];
    }
    
    void reflect() {
        // 类的反射
        NSString *str = @"Person";
        Class class = NSClassFromString(str);
        
        Person *person = [[class alloc] init];
        NSLog(@"%@", person);
        
        
        // Class变成字符串
        NSString *name =  NSStringFromClass([Person class]);
        
        
        // 方法的反射
        NSString *method = @"test";
        SEL selector = NSSelectorFromString(method);
        [person performSelector:selector];
        
        // 将SEL转换为字符串
        NSString *selectorName = NSStringFromSelector(selector);
        
        
        [person release];
    }
  • 相关阅读:
    lr11_Analysis_Options选项介绍:
    lr11_Controller_Options选项介绍:
    ArcGIS Python 文件扩展名过滤器设置
    arcgis python xlstoshp
    arcgis python 标注
    ArcGIS Python 唯一值专题
    arcpy 获得是否为布局mxd.activeView
    python 度分秒转度
    我的新书,ArcGIS从0到1,京东接受预定,有160个视频,851分钟
    python 数字转字符保留几位小数 by gisoracle
  • 原文地址:https://www.cnblogs.com/yif1991/p/5068293.html
Copyright © 2020-2023  润新知