英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论