所以,问题就在标题里。我使用‘从不’关键字在类中声明了一些属性,这样我就可以在构造函数中只设置一次这些属性的值。但是,我得到以下错误:
无法访问字段或标识符%name%以写入
有问题的代码示例:
class TreeAbility
{
public var id(default, never):String;
public var maxLvl(default, never):Int;
public function new(id:String, maxLvl:Int)
{
Assert.assert(maxLvl > 0);
this.id = id; (*)
this.maxLvl = maxLvl; (*)
this.currentLvl = 0;
}
}
标记为(*)的行将引发访问错误
发布于 2017-10-12 08:58:59
我认为“从不写”属性意味着永远不允许写入/设置变量,即使在构造函数中也是如此。请参阅:https://haxe.org/manual/class-field-property.html
也许你正在寻找最后一个关键字,它是在Haxe 4中出现的。例如,它允许只从类构造函数分配给变量。在此确认:https://haxe.org/download/version/4.0.0-preview.2/和https://github.com/HaxeFoundation/haxe/issues/6584
https://stackoverflow.com/questions/46714697
复制