设置 Objective-C 实例变量值

发布于 2025-01-01 08:19:24 字数 506 浏览 0 评论 0原文

我有以下代码:

@implementation Fraction   
{  
      int   numerator;  
      int   denominator;  
}  

    -(void) setNumerator: (int) n   
{  
    numerator = n;   
}  
    -(void) setDenominator: (int) d   
{  
    denominator = d;   
}
@end

我想知道为什么分子必须同时具有“n”和“分子”?例如,为什么你不能直接设置

-(void) setNumerator: (int) numerator

为:

-(void) setNumerator: (int) n
     numerator = n;
}

抱歉,如果这是一个基本问题,但我从头开始,没有编程经验。

I have the following code:

@implementation Fraction   
{  
      int   numerator;  
      int   denominator;  
}  

    -(void) setNumerator: (int) n   
{  
    numerator = n;   
}  
    -(void) setDenominator: (int) d   
{  
    denominator = d;   
}
@end

I was wondering why you have to have both "n" and "numerator" for the numerator? For example why can't you just set

-(void) setNumerator: (int) numerator

as apposed to:

-(void) setNumerator: (int) n
     numerator = n;
}

Sorry if this is such a basic question but I'm starting from the beginning with no programming experience.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2025-01-08 08:19:24

setNumerator 方法用于告诉 Fraction 对象应在名为 numerator 的变量中保存什么值。正如“在这里,分数先生,请使用值n作为您的分子。”在该方法的实现中,代码必须处理两个不同的概念:包含新值的参数变量n和必须更改为的实例变量numerator n 中的值;因此需要两个不同的名称。该行

numerator = n;

字面意思是“将n中的数字复制到变量numerator中”。

请记住,交易分为两部分。假设我是一个 Fraction 对象,并且您希望我将分子设置为 4。您说“将分子设置为 4,或 setNumerator(4)”,这很好,因为您人类,你可以选择你想要的任何数字。

但作为一个低级的 Objective-C 对象,我的所有代码都是在过去的某个时间编写的;它是在值4在你眼前闪烁之前写下的。因此 setNumerator() 的代码必须是通用的;它必须写成“将分子设置为人类想要的任何值 - 称之为n”。请记住,我的实际变量分子对您来说是隐藏的;你所能做的就是调用我的方法,由我来设置变量。

这就是为什么必须编写方法以使用抽象名称 - n - 作为值,因为编写方法时,值是未知的。

The setNumerator method is used to tell a Fraction object what value it should hold in the variable named numerator. As in "Here, Mr. Fraction, please use the value n for your numerator." In the implementation of this method, the code has to deal with two different concepts: the parameter variable n containing the new value, and the instance variable numerator which must be changed to the value in n; hence the need for two different names. The line

numerator = n;

literally means "copy the number in n into the variable numerator."

Remember, there are two halves to the transaction. Say I'm a Fraction object, and you want me to set my numerator to 4. You say "Set your numerator to 4, or setNumerator(4)", which is fine, because you're human, and you get to choose whatever number you want.

But as a lowly Objective-C object, all my code was written some time in the past; it was written before the value 4 was even a twinkle in your eye. So the code for setNumerator() has to be generic; it has to be written to say "set numerator to whatever value the human wants -- call it n". Remember, my actual variable numerator is hidden from you; all you can do is call my method, and it's up to me to set the variable.

That's why the method must be written to use an abstract name -- n -- for the value, since when the method is written, the value is unknown.

如若梦似彩虹 2025-01-08 08:19:24

我认为您可能正在寻找的概念称为属性。 看看这里此处此处。属性将允许您设置成员值,而无需命名参数或调用方法。您只需为该属性分配一个值即可。

通过函数调用,您必须命名要传递到函数中的参数,以便可以将其分配给类成员。

I think the concept you may be looking is called properties. Take a look here and here and here. Properties will allow you to set member values without having to name a parameter or call a method. You simply assign a value to the property.

With a function call you have to name the parameter that you are passing into the function so that you can assign it to the class member.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文