+ (void)initialize vs 静态构造方法

本文详细探讨了在面向对象编程中,初始化与继承机制的工作原理。特别关注了类的初始化时机,即当类首次接收消息时如何进行初始化,并讨论了初始化过程中父类与子类的执行顺序。此外还涉及了特殊情况下的实现细节。

在继承体系中,多个子类的引用,父类缺省执行一次;

特殊情况:

1)子类没有实现、调用父类方法;

2)子类显示调用父类;

3)存在分类实现,分类实现覆盖本体。

 

Initializes the class before it receives its first message.

The runtime sends initialize to each class in a program just before the class, or any class that inherits from it, is sent its first message from within the program. Superclasses receive this message before their subclasses.

The runtime sends the initialize message to classes in a thread-safe manner. That is, initialize is run by the first thread to send a message to a class, and any other thread that tries to send a message to that class will block until initialize completes.

The superclass implementation may be called multiple times if subclasses do not implement initialize—the runtime will call the inherited implementation—or if subclasses explicitly call [super initialize]. If you want to protect yourself from being run multiple times, you can structure your implementation along these lines:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值