Apex:使用索引符号设置属性

Apex: Setting Properties with the Indexing Notation

是否可以像这样在 class 的实例上设置 属性;

MyCls item = new MyCls();

item['propName'] = propValue;

没有。 Apex 不支持任何对象的索引符号 - 自定义 类、Map 实例或 sObjects。

您可以在 Map and sObject 内置 类 上使用 get()put() 方法按名称或 Map 值访问字段。但是,在 sObject 上这样做会使您失去 compile-time 字段检查,并且通常需要在 statically-typed Apex 中进行大量转换。最好尽可能使用标准访问权限。

这不适用于自定义 类,除非您实现自己的访问器方法。