【发布时间】:2015-07-10 20:34:30
【问题描述】:
基本上,我想显示和利用在超类中声明和设置的变量。
这是将变量设置为其字符的代码:
- (IBAction)additionSelect:(UIButton *)sender {
_operation = '+';
_operationChosenMessage.text = [NSString stringWithFormat:@"You chose %c", _operation];
}
- (IBAction)subtractionSelect:(UIButton *)sender {
_operation = '-';
_operationChosenMessage.text = [NSString stringWithFormat:@"You chose Subtraction"];
}
这是显示变量的使用:
if([self operation] == '+'){
if (_firstNumber + _secondNumber == [_writeNumber.text intValue]) {
_score++;
}
}else if ([self operation] == '-'){
if (_firstNumber - _secondNumber == [_writeNumber.text intValue]) {
_score++;
}
}
_firstNumber = (arc4random() % 40) - 20;
_secondNumber= (arc4random() % 40) - 20;
_displayEquation.text = [NSString stringWithFormat:@"%i %c %i", _firstNumber,self.operation,_secondNumber];
_operation 是在超类(1st sn-p) 中声明和设置的 char 变量,我想在子类中使用它。
【问题讨论】:
-
[自操作]应该返回什么?我认为这是一个 void 方法,它什么也不返回
-
一个字符,可以是 + 或 -,具体取决于您之前在情节提要中选择的按钮。
-
可以发一下【自操作】的代码吗?
-
重要的是如何定义操作 ivar/property。如果基类定义了操作属性并且[auto]synthesizes _operation ivar,假设它没有重新声明操作,在子类中访问它应该没有问题。请发布您的超类和子类的接口。