【发布时间】:2011-02-15 10:00:09
【问题描述】:
我正在阅读 Stephen Kochan 的“Objective-C 编程”(我是 n0000b)。在练习 4-8 之前,一切对我来说都是显而易见的。
它要求我修改本章前面创建的“累加器”类中的方法,以便在使用其中一种数学方法(基本上它是一个计算器)时“返回”累加器的值。我最初认为这意味着我希望程序在使用其中一种方法时显示结果(+、-、*、/),所以我将其设置为这样做,所以每一行都显示累积结果而不是只是最终结果:
[deskCalc setAccumulator: 0.0];
[deskCalc add: 200.]; //the result is displayed
[deskCalc divide: 100.0]; //the result is displayed
[deskCalc subtract: 1.0]; //the result is displayed
[deskCalc multiply: 5]; //the result is displayed
NSLog (@"The result is %g", [deskCalc accumulator]);
但是在查找了其他人的解决方案之后,似乎“返回累加器的值”意味着不同的东西。
有人可以向我描述返回值的含义以及它的用途吗?我已经看过前一章几次,但我仍然不清楚这将如何使程序的行为有所不同。
谢谢! -安德鲁
【问题讨论】: