Crystal是否有像Ruby中的属性访问方法?

huangapple go评论64阅读模式
英文:

Does Crystal have attribute accessor methods like in Ruby?

问题

Crystal编程语言是否有与Ruby的属性访问器方法相对应的功能?更具体地说,Crystal是否有以下等效功能?

  • attr_accessor
  • attr_reader
  • attr_writer
英文:

Does the Crystal programming language have an equivalent to Ruby's attribute accessor methods? More specifically, does Crystal have equivalents to the following?

  • attr_accessor
  • attr_reader
  • attr_writer

?

答案1

得分: 7

是的,它们被定义为宏。

基本上:

ruby crystal
attr_accessor property
attr_reader getter
attr_writer setter

示例

class Person
  property name
end

等同于

class Person
  def name=(@name)
  end

  def name
    @name
  end
end

更多详情请参阅参考文档

英文:

Yes, they're defined as macro.

Basically:

ruby crystal
attr_accessor property
attr_reader getter
attr_writer setter

Example

class Person
  property name
end

is equal to

class Person
  def name=(@name)
  end

  def name
    @name
  end
end

For more details see the Reference

huangapple
  • 本文由 发表于 2023年5月11日 04:22:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定