【发布时间】:2014-04-18 19:49:42
【问题描述】:
我正在学习 Objective-C,作为课程之一,我们创建了一个 Employee 类及其超类 Person,以及一个 Asset 类,其实例由 Employee 所有。为了测试这些类及其实例函数,我编写了以下代码
int main(int argc, const char *argv[]){
@autoreleasepool{
NSMutableArray *employees = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++){
Employee *person = [[Employee alloc]init];
[person setWeightInKilos:90 + i];
[person setHeightInMeters:1.8 - i/10.00];
[person setEmployeeID:i];
[employees addObject:person];
}
.../*code continues from here, to assign [person]
's at random indexes in *employees, instances of the Asset class */
}
}
我想知道 setting 和 [person] 使用一个方法有什么好处或坏处,而后者又调用其他方法,如下所示:
main.m:
....
for (int i = 0; i < 10; i++){
Employee *person = [[Employee alloc]init];
[person setWeightInKilos:90 + i withAHeightOf:1.8 - i/10.00 andId:i];
[employees addObject:person];
}
....
谢谢。
【问题讨论】:
-
如果需要在创建对象后立即设置该信息,请让您的初始化程序将其作为参数。
标签: objective-c parameters messages nested