问题标签 [accessor]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
721 浏览

ruby - 自定义 attr_reader 以进行属性的惰性实例化

(大编辑,我已经完成了其中的一部分……)我一直在破解,我想出了一个方法来指定在读取属性之前需要完成的事情:

但是changethings不被认为是一种方法——有人有什么想法吗?

0 投票
2 回答
270 浏览

cocoa - Cocoa 是否提供了将键转换为“设置”属性访问器的内置方法?

Cocoa 是否提供内置方法来将键字符串转换为格式正确的 set 属性访问器?IE"lineSpacing" -> setLineSpacing:

例如:

甚至:

背景:

我正在研究一种方法,该方法将使用存储在字典中的属性值按名称设置许多属性。我不能使用setObject:forKey,因为我也将此方法用于内置类型的属性,例如NSIntegerand CGFloat。这是我用于 CGFloat 的方法的示例,用于说明:

此方法适用于“颜色”或“宽度”等单字属性,但不适用于“lineSpacing”或“meaningOfLifeTheUniverseAndEverything”等多字属性,因为该capitalize方法将第一个字母大写,同时将所有后续字母设置为小写。使用“lineSpacing”作为键调用上述方法会导致失败,因为正确的属性设置器将是setLineSpacing:而不是setLinespacing:

0 投票
2 回答
137 浏览

c# - 使用访问器公开类属性

我不知道描述我的问题的正确技术术语,所以我举个例子:

问题是如果我想访问Publicvect.X我会得到错误Cannot modify the return value of 'Publicvect' because it is not a variable。有没有解决的办法?还是我只需要Publicvect = new Point(NewX, Publicvect.Y);永远做?

0 投票
4 回答
18103 浏览

c# - when and why to use C# accessor methods

Possible Duplicate:
C# - When to use properties instead of functions

I am trying to understand when and why to use "getters" and "setters"

would someone please provide some guidance.

What is the difference between the following constructs - please look in terms of accessor methods only.

0 投票
3 回答
447 浏览

c# - Get-Set 访问器功能因 get-set 关键字的存在而异

我目前正在实现 RSA 算法的穷人版本,我希望质数 d、e、m 和 n 是只读的,因为它们将在构造函数体内自动生成。但是,当我键入时,我会得到两个不同的结果:

或者

阅读 Accelarated C# 一书,我得到的印象是可以使用自动实现的属性来实现私有集合函数。结果我也可以在构造函数本身中做到这一点,但仅限于第一个版本。

阅读 C# 3.0 标准,它说:

A property that has both a get accessor and a set accessor is a read-write property, a property that has only a get accessor is a read-only property, and a property that has only a set accessor is a write-only property.

然而,他们的行为并不相同。

简单的问题:为什么当我显式声明时可以初始化构造函数中的值private set,但如果我隐式执行则不能?这里有什么区别?

0 投票
8 回答
586 浏览

c# - C# 访问器应该使用私有变量还是动态计算?

哪个是更好的编程实践,为什么?

我有这样的课:

其中 average_count 应该是 read_only 并给出计数/时间的计算。

将访问器编写为:

或者我应该做类似的事情:

在时间和计数集访问器中更新_avg_count?

似乎第一个更容易阅读,但如果经常访问 average_count 可能会更慢。编译器优化是否会使差异变得微不足道?

0 投票
1 回答
223 浏览

iphone - iPhone CoreData properties: changes to managedObjects are too slow

I have a CoreData model in my iPhone app, which is linked to a SQL Database with more than 50k records. When I generate the records classes, Xcode uses the @dynamic directive for properties. I have a property named "ISFAV", NSNumber type (CoreData does not use BOOL or Integer, it uses object types). Being short, I change the ISFAV property when the user taps on a button in this way:

record.ISFAV=[NSNumber numberWithInt:1];

else record.ISFAV=[NSNumber numberWithInt:0];

Quite simple. But if I try to tap many times on the same button sequentially, the iPhone takes too much time (the button remains in the hold state for a time that increase progressively). This happens even if I change record, adding\removing many records from favorites sequentially (instead of adding\deleting the same record from favorites).

If I change the original accessor method to @synthesize, the problem seems to be solved.

Is it correct to use the synthesize directive for accessor methods in CoreData?

Thank you very much!

@edit Using the synthesize directive, no changes are made to the CoreData model when I save the context :-\ The problem is still unsolved :-\

0 投票
4 回答
206 浏览

c# - 只允许在构造函数中设置值的 set 访问器的名称是什么?

任何想法 KEYWORD 是什么?我到处搜索,但很难在 google 中找到 get/set 访问器。

0 投票
7 回答
330 浏览

java - 为什么调用方法来设置变量比在构造函数中正常设置变量更好?

我在一本 Java 书籍中做一个项目,并遇到了这个代码示例。这本书的作者说,与其直接在我的构造函数中初始化 X 和 Y,我可以调用类的 setLocation() 方法来代替。不幸的是,我不再有这本书来具体解释为什么这会更好。我对 Java 的经验不是很丰富,但它不只是......更简单地直接分配值而不用担心另一个函数调用吗?

0 投票
10 回答
82512 浏览

c++ - C++ 中访问器方法(getter 和 setter)的约定

在 SO 上已经提出了几个关于 C++ 中访问器方法的问题,但没有一个能够满足我对这个问题的好奇心。

我尽可能避免使用访问器,因为像 Stroustrup 和其他著名的程序员一样,我认为一个包含许多访问器的类是糟糕的 OO 的标志。在 C++ 中,在大多数情况下,我可以为类添加更多责任或使用friend 关键字来避免它们。然而在某些情况下,您确实需要访问特定的班级成员。

有几种可能性:

1. 完全不要使用访问器

我们可以将各自的成员变量公开。这在 Java 中是不行的,但在 C++ 社区中似乎没问题。但是,我有点担心应该返回显式副本或对对象的只读(const)引用的情况,这是否夸大了?

2. 使用 Java 风格的 get/set 方法

我不确定它是否来自Java,但我的意思是:

3. 使用客观的 C 风格的 get/set 方法

这有点奇怪,但显然越来越普遍:

为了让它工作,你必须为你的成员变量找到一个不同的名字。有些人在后面加上下划线,有些人在前面加上“m_”。我也不喜欢。

你使用哪种风格,为什么?