An introduction to Objective-C Meta Class

转自:http://www.cnblogs.com/qytan36/archive/2011/04/19/2021359.html


An introduction to Objective-C Meta Class

First, let's have a look at the base class NSObject.
@interface NSObject <NSObject> {    //implement protocol NSObject
    Class    isa;    //point to meta class, all instances of NSObject share the same meta class.
}
+ (void)load;
+ (void)initialize;
- (id)init;

+ (id)new;
+ (id)allocWithZone:(NSZone *)zone;
+ (id)alloc;
- (void)dealloc;

+ (Class)superclass;
+ (Class)class;
...

@end

Class is defined as: 
typedef struct objc_class *Class;
struct objc_class {
    Class isa;

#if !__OBJC2__
    Class super_class                                        OBJC2_UNAVAILABLE;
    const char *name                                         OBJC2_UNAVAILABLE;
    long version                                             OBJC2_UNAVAILABLE;
    long info                                                OBJC2_UNAVAILABLE;
    long instance_size                                       OBJC2_UNAVAILABLE;
    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
#endif

} OBJC2_UNAVAILABLE;
objc_class is the meta class. 

id is defined as:
typedef struct objc_object {
    Class isa;
} *id;
id is an pointer pointed to struct obj_object very similar with the definition of NSObject.



//class TestA
@interface TestA : NSObject {
    
    int a;

}

+ (id)alloc;
+ (void)initialize;

@end

@implementation TestA

+ (void)load
{
    printf("load TestA. %s %#x\n", [[self description] UTF8String], self);
}

+ (id)alloc
{
    self = [super alloc];
    printf("alloc TestA. %s %#x\n", [[self description] UTF8String], self);
    return self;
}

+ (void)initialize
{
    printf("initialize TestA. %s %#x\n", [[self description] UTF8String], self);
}

- (id)init
{
    self = [super init];
    if(self)
    {
        printf("init TestA. %s %#x isa=%#x\n", [[self description] UTF8String], self, isa);
    }
    return self;
}

@end

//End of TestA

//class TestB
@interface TestB : TestA {

    int b;
    
}

+ (id)alloc;

+ (void)initialize;

@end



@implementation TestB

+ (void)load
{
    printf("load TestB. %s %#x\n", [[self description] UTF8String], self);
}

+ (id)alloc
{
    self = [super alloc];
    printf("alloc TestB. %s %#x\n", [[self description] UTF8String], self);
    return self;
}

+ (void)initialize
{
    printf("initialize TestB. %s %#x\n", [[self description] UTF8String], self);
}

- (id)init
{
    self = [super init];
    if(self)
    {
        printf("init TestB. %s %#x isa=%#x\n", [[self description] UTF8String], self, isa);
    }
    return self;
}
@end
//End of TestB

//Testing example
TestB *b1 = [[TestB alloc] init];
TestB *b2 = [[TestB alloc] init];
TestA *a1 = [[TestA alloc] init];

Output:
initialize TestA. TestA 0xd634
load TestA. TestA 0xd634
initialize TestB. TestB 0xd65c
load TestB. TestB 0xd65c
alloc TestA. <TestB: 0x4e317f0> 0x4e317f0
alloc TestB. <TestB: 0x4e317f0> 0x4e317f0
init TestA. <TestB: 0x4e317f0> 0x4e317f0 isa=0xd65c
init TestB. <TestB: 0x4e317f0> 0x4e317f0 isa=0xd65c
alloc TestA. <TestB: 0x4e369e0> 0x4e369e0
alloc TestB. <TestB: 0x4e369e0> 0x4e369e0
init TestA. <TestB: 0x4e369e0> 0x4e369e0 isa=0xd65c
init TestB. <TestB: 0x4e369e0> 0x4e369e0 isa=0xd65c
alloc TestA. <TestA: 0x4e34930> 0x4e34930
init TestA. <TestA: 0x4e34930> 0x4e34930 isa=0xd634

From the outputs, some rules can be concluded:
1. In class method, such as initialize, load, alloc. self is the address of meta class, in another word, a pointer to struct objc_class.
2. The first time a class is referenced, its meta class will only be initialized and loaded once.
3. All instances of class objects share a meta class.
4. TestB inherits from TestA. The meta class address of TestB is 0xd65c. The meta class address of TestA is 0xd634. So TestB.isa->super_class = 0xd634. so TestB can call super's methods. All methods and meta infomations are stored in isa.
【评估多目标跟踪方法】9个高度敏捷目标在编队中的轨迹和测量研究(Matlab代码实现)内容概要:本文围绕“评估多目标跟踪方法”,重点研究9个高度敏捷目标在编队飞行中的轨迹生成与测量过程,并提供完整的Matlab代码实现。文中详细模拟了目标的动态行为、运动约束及编队结构,通过仿真获取目标的状态信息与观测数据,用于验证和比较不同多目标跟踪算法的性能。研究内容涵盖轨迹建模、噪声处理、传感器测量模拟以及数据可视化等关键技术环节,旨在为雷达、无人机编队、自动驾驶等领域的多目标跟踪系统提供可复现的测试基准。; 适合人群:具备一定Matlab编程基础,从事控制工程、自动化、航空航天、智能交通或人工智能等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于多目标跟踪算法(如卡尔曼滤波、粒子滤波、GM-CPHD等)的性能评估与对比实验;②作为无人机编队、空中交通监控等应用场景下的轨迹仿真与传感器数据分析的教学与研究平台;③支持对高度机动目标在复杂编队下的可观测性与跟踪精度进行深入分析。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注轨迹生成逻辑与测量模型构建部分,可通过修改目标数量、运动参数或噪声水平来拓展实验场景,进一步提升对多目标跟踪系统设计与评估的理解。
本软件实现了一种基于时域有限差分法结合时间反转算法的微波成像技术,旨在应用于乳腺癌的早期筛查。其核心流程分为三个主要步骤:数据采集、信号处理与三维可视化。 首先,用户需分别执行“WithTumor.m”与“WithoutTumor.m”两个脚本。这两个程序将在模拟生成的三维生物组织环境中进行电磁仿真,分别采集包含肿瘤模型与不包含肿瘤模型的场景下的原始场数据。所获取的数据将自动存储为“withtumor.mat”与“withouttumor.mat”两个数据文件。 随后,运行主算法脚本“TR.m”。该程序将加载上述两组数据,并实施时间反转算法。算法的具体过程是:提取两组仿真信号之间的差异成分,通过一组专门设计的数字滤波器对差异信号进行增强与净化处理,随后在数值模拟的同一组织环境中进行时间反向的电磁波传播计算。 在算法迭代计算过程中,系统会按预设的周期(每n次迭代)自动生成并显示三维模拟空间内特定二维切面的电场强度分布图。通过对比观察这些动态更新的二维场分布图像,用户有望直观地识别出由肿瘤组织引起的异常电磁散射特征,从而实现病灶的视觉定位。 关于软件的具体配置要求、参数设置方法以及更深入的技术细节,请参阅软件包内附的说明文档。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值