gpt4 book ai didi

用外行的话来说是 Ruby self 吗?

转载 作者:数据小太阳 更新时间:2023-10-29 07:24:08 25 4
gpt4 key购买 nike

Ruby self 什么时候引用 Object 什么时候 self 引用 Ruby 类?有例子的解释会很棒。没有解决这个问题。

最佳答案

类实际上是对象本身。假设我有一个类 Person,这实际上是 Class 的一个实例。因此,您可以让 self 引用 Article 的实例,或者让 self 引用类 Article 的实例。

在我能想到的最简单的例子中:

class Person
def initialize
p "Info about Person Instance"
p self
p self.class
end

p "Info about Person Class"
p self
p self.class
end


person = Person.new

它打印:

"Info about Person Class"
Person
Class
"Info about Person Instance"
#<Person:0x0000010086cf58>
Person

要了解更多关于 self 的信息,I highly recommend read this.

关于用外行的话来说是 Ruby self 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5734404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com