• 全部
...

ios 中子类初始化时需要调用父类初始化 [super init] ,不调用会怎么样?

li_de 2015-04-02 02:12:59
ios初学者,希望大侠能给我理论指导一下!
...全文
给本帖投票
758 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Young_咯噔 2016-05-11
  • 打赏
  • 举报
回复
We just said that init methods like this one can return totally different objects. Remember that instance variables are found at a memory location that’s a fixed distance from the hidden self parameter. If a new object is returned from an init method, we need to update self so that any subsequent instance variable references affect the right places in memory. That’s why we need the self = [super init] assignment. Keep in mind that this assignment affects the value of self only for this method. It doesn’t change anything outside the method’s scope.
ISSJewellery 2015-04-09
  • 打赏
  • 举报
回复
对三楼做点补充 你不调用[super init]那就不做这一行啊,简单的说就是这个子类对父类有继承属性,方法,但是不做父类初始化,(一般我们会在父类里对property做些初始化,或者调用一些方法什么的,但是你错过了这句,这些动作完全跳过)
ReyZhang 2015-04-03
  • 打赏
  • 举报
回复
你应该是对面向对象编程不了解。 在oc中基本上所有的类都是继承自NSObject 而来,而继承是面向对象编程中一个很重要的特性。 子类继承父类,并获得父类相关的属性和方法,所以在子类初始化时必须首先调用父类的初始化方法,以实现父类相关资源的初始化。
Bannings 2015-04-02
  • 打赏
  • 举报
回复
alloc方法只用来分配内存,而init是对对象进行初始化方法,两个方法合起来等于Java的new或OC的new。如果不init的话,内存中还有之前的数据
li_de 2015-04-02
  • 打赏
  • 举报
回复
自己先顶一下!

29,049

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部