I just started to learn iOS programming and I have a problem with inheritance. There are 2 files.
我刚开始学习iOS编程,我有继承问题。有两个文件。
First file
第一个文件
Header
头
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
int x;
}
@end
Implementation:
实现:
#import "ViewController.h"
#import "NewClass.h"
@implementation ViewController
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
x = 999;
NewClass *myClass = [[[NewClass alloc] init] autorelease];
}
@end
Second file
第二个文件
Header:
标题:
#import "ViewController.h"
@interface NewClass : ViewController
@end
Implementation:
实现:
#import "NewClass.h"
@implementation NewClass
-(id)init {
self = [super init];
if (self != nil) {
NSLog(@"%i",x);
}
return self;
}
@end
In ViewController I set x to 999, and in NewClass I want to get it, but when I call NSLog(@"%i",x);
it gives me 0.
在ViewController中,我将x设为999,在NewClass中,我想要得到它,但是当我调用NSLog(@ % I,x);它给我0。
Where did I make a mistake?
我哪里出错了?
3 个解决方案
#1
8
You have a timing problem.
你有时间问题。
-
The
init
method gets called first (at all levels of the inheritance hierarchy, so in bothViewController
andNewClass
). This is when you print out your value ofx
, when it is still zero.init方法首先被调用(在继承层次结构的所有级别上,所以在ViewController和NewClass中)。这是当你打印出x的值时,它还是0。
-
The
viewDidLoad
method only gets called much later, generally at a point after a view controller's view has been added to a superview. It's functionality that's specific to theUIViewController
class.viewDidLoad方法只会在以后调用,通常是在视图控制器的视图添加到父视图之后。它是特定于UIViewController类的功能。
To make your example work, put an init
method in your ViewController
class that looks like the one in your NewClass
class, and set x there.
要使示例正常工作,请在ViewController类中放置一个init方法,该方法与NewClass类中的方法类似,并在那里设置x。
Also, you don't need to create a NewClass
instance within ViewController
. When you create a NewClass
object, it is automatically a ViewController
as well. In the same way that a dog is an animal automatically, and so is a cat. When you create a dog, you don't want to create an animal as well!
同样,您不需要在ViewController中创建一个NewClass实例。当你创建一个NewClass对象时,它也是一个ViewController。就像狗是自动的动物一样,猫也是一样。当你创造一个狗,你也不想创造一个动物!
As sidyll says, you should probably do a bit more reading about how inheritance works, I'm afraid!
正如sidyll所说,恐怕你应该多读一些关于遗传是如何运作的文章。
#2
6
You need to review you OOP concepts. Object-Oriented Programming with Objective-C is a must.
您需要检查OOP概念。必须使用Objective-C进行面向对象编程。
Your class NewClass
indeed inherits the x
variable, but not it's value. When you create an instance of it, you're creating a shiny new instance whose values have nothing to do with the parent class.
类NewClass确实继承了x变量,但不是它的值。当您创建它的实例时,您正在创建一个闪亮的新实例,其值与父类无关。
Another point of view to help you is that x
was set in a object of ViewController
class. The NewClass
inherits from ViewController
class, not from an arbitrary instance (object, where you set x
).
另一个帮助你的观点是x被设置在一个ViewController类的对象中。NewClass继承自ViewController类,而不是从任意实例(对象,在其中设置x)继承。
#3
0
That's because -viewDidLoad
is not called until well after -init
returns. Your superclass should do configuration like that in its -init
method.
这是因为-viewDidLoad直到-init返回后才被调用。您的超类应该在其-init方法中执行类似的配置。
#1
8
You have a timing problem.
你有时间问题。
-
The
init
method gets called first (at all levels of the inheritance hierarchy, so in bothViewController
andNewClass
). This is when you print out your value ofx
, when it is still zero.init方法首先被调用(在继承层次结构的所有级别上,所以在ViewController和NewClass中)。这是当你打印出x的值时,它还是0。
-
The
viewDidLoad
method only gets called much later, generally at a point after a view controller's view has been added to a superview. It's functionality that's specific to theUIViewController
class.viewDidLoad方法只会在以后调用,通常是在视图控制器的视图添加到父视图之后。它是特定于UIViewController类的功能。
To make your example work, put an init
method in your ViewController
class that looks like the one in your NewClass
class, and set x there.
要使示例正常工作,请在ViewController类中放置一个init方法,该方法与NewClass类中的方法类似,并在那里设置x。
Also, you don't need to create a NewClass
instance within ViewController
. When you create a NewClass
object, it is automatically a ViewController
as well. In the same way that a dog is an animal automatically, and so is a cat. When you create a dog, you don't want to create an animal as well!
同样,您不需要在ViewController中创建一个NewClass实例。当你创建一个NewClass对象时,它也是一个ViewController。就像狗是自动的动物一样,猫也是一样。当你创造一个狗,你也不想创造一个动物!
As sidyll says, you should probably do a bit more reading about how inheritance works, I'm afraid!
正如sidyll所说,恐怕你应该多读一些关于遗传是如何运作的文章。
#2
6
You need to review you OOP concepts. Object-Oriented Programming with Objective-C is a must.
您需要检查OOP概念。必须使用Objective-C进行面向对象编程。
Your class NewClass
indeed inherits the x
variable, but not it's value. When you create an instance of it, you're creating a shiny new instance whose values have nothing to do with the parent class.
类NewClass确实继承了x变量,但不是它的值。当您创建它的实例时,您正在创建一个闪亮的新实例,其值与父类无关。
Another point of view to help you is that x
was set in a object of ViewController
class. The NewClass
inherits from ViewController
class, not from an arbitrary instance (object, where you set x
).
另一个帮助你的观点是x被设置在一个ViewController类的对象中。NewClass继承自ViewController类,而不是从任意实例(对象,在其中设置x)继承。
#3
0
That's because -viewDidLoad
is not called until well after -init
returns. Your superclass should do configuration like that in its -init
method.
这是因为-viewDidLoad直到-init返回后才被调用。您的超类应该在其-init方法中执行类似的配置。