我的实例变量secondNumber没有声明自己。为什么不? (我是编程新手,所以请耐心等待。) 在Xcode中,错误表示“解析问题:预期”;'在声明清单的末尾。
// main.m
// Subtraction
//
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NumberSub: NSObject
{
int firstNumber
int secondNumber
int result
}
-(void) print;
-(void) setFirstNumber: (int) f;
-(void) setSecondNumber: (int) s;
-(void) resetAll;
-(void) printResult;
@end
@implementation NumberSub
-(void) setFirstNumber: (int) f;
{
firstNumber = f;
}
-(void) setSecondNumber: (int) s
{
secondNumber = s;
}
-(void) print
{
NSLog(@"%i - %i = ", firstNumber, secondNumber);
}
-(void) printResult
{
NSLog(@"%i", firstNumber - secondNumber);
}
-(void) resetAll
{
firstNumber = 0;
secondNumber = 0;
}
@end
int main (int argc, char *argv[])
{
NumberSub *subtractNumber = [[NumberSub = alloc]init];
[subtractNumber setFirstNumber: 10];
[subtractNumber setSecondNumber: 5];
[subtractNumber print];
[subtractNumber printResult];
[subtractNumber release];
return 0;
}
答案 0 :(得分:1)
@interface NumberSub: NSObject
{
int firstNumber;
int secondNumber;
int result ;
}