我是目标c和java背景的新手,我想知道基类中目标c中init()
方法/函数的含义是什么,同时实现继承?
这是java中的一种构造函数吗?是否必须仅使用init()
方法来创建和使用对象?我们是否也可以使用其他方法而不是init()
?
最后,为什么init()
的返回类型为id
?
答案 0 :(得分:0)
所以,如果它是客观的c,它是init而不是init():)只是说......有点大事...... 当你在做一个
时,请参阅javanew MyClass()
发生了两件事:
现在,在Objective-C中,这两件事分为两种方法: - 分配 - init
所以当你想要创建一个新对象时,你必须调用这两个方法
MyClass * a = [MyClass alloc]; // this will return a pointer to a space in memory big enough to store an instance of MyClass
a = [a init]; // this will call the init method on a and return the pointer initialized with all iVar to 0 plus whatever you did by yourself in the init method.
通常,它是在一行中完成的
MyClass * a = [[MyClass alloc] init];
这就是为什么使用init返回实际实例
的实例类是如此方便