我(基本上)是编程新手,并试图学习Objective-C。我正在研究一个单位转换程序,它使用类的实例来存储和转换单位。例如,如果我想将1英尺转换为英寸,程序将执行以下操作:
- 创建一个Length类(myLength)的新实例
- 将值和单位存储在类(myMass)的新实例中
- 调用实例方法将值转换为新单位
我的长度类工作良好,正如我所期望的。
现在我想创建一个名为Area的新类,它创建Length类的两个实例,并使用Length类执行转换。但是,当我试图从Area中声明Length类的新实例时。m,我得到警告"长度未声明"。
是否可以在实例方法中创建不同类的实例?更具体地说,我可以从Area类实例方法中创建两个Length类的实例吗?
(如果我用错了术语,请提前原谅)。
下面是Area中的代码片段。给我添麻烦的M:
@implementation Area
-(void) setCompound: (double) myVal unit1: (char) c1 unit2: (char) c2
{
// Create two new instances of the Length class
// THE FOLLOWING TWO LINES ARE GIVING ME TROUBLE
Length * aLength = [[Length alloc] init];
Length * bLength = [[Length alloc] init];
[aLength setLength:myVal units: c1]; // Set the value and units of first length
[bLength setLength: 1 units: c2]; // Set the value and units of the second length
}
@end
您需要在Area.h中导入Length头文件
#import "Length.h"