类声明是红宝石中的洗眼剂吗?一切真的都是面向对象的吗?
class Person
def name
puts "Dave"
end
end
puts Person.object_id
访问方法只有两种方式:
1) Someclass.method(如果是类方法)。 #where Someclass 是一个类。
2) 和 Object.method(当被访问的方法是在类内部声明的常规方法时)。 Object 是类的实例。
它遵循 Object.method 模式,那么这是否意味着 Person 类确实是一个对象?
或者object_id是一个类方法?后者似乎不太可能,因为类方法不能继承到实例中。但是当我们这样做时:
a = Person.new
a.methods.include?("object_id") # this produces true
a 是 Person 类的实例,因此 object_id 不能是类方法。
class Person
def name
puts "Dave"
end
end
puts Person.object_id
There are only two ways of accessing methods :
1) Someclass.method in case of class methods. #where Someclass is a class.
2) and Object.method when the method being accessed is a regular method declared inside a class. and Object is an instance of a class.
It follows the pattern Object.method so, does it mean Person class is really an object?
or object_id is a class method? The latter seems unlikely because class methods cannot be inherited into an instance. but when we do something like this :
a = Person.new
a.methods.include?("object_id") # this produces true
a is an instance of Person class so object_id cannot be a class method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,Ruby 类是对象:
Yes, Ruby classes are objects:
是的,Ruby 中的类是类
Class
的实例。事实上,您可以使用以下命令创建相同的类:然后,您只需键入 Person.new.name ,它就会像您的类一样工作。
检查 Person 是否是类
Class
的实例就像输入 replPerson.class
一样简单,然后您会得到Class
作为返回。Yes, classes in Ruby are instances of class
Class
. In fact, you can create the same class just with:Then, you can just type
Person.new.name
and it will work exactly as your class.Checking that Person is an instance of class
Class
is as easy as typing in your replPerson.class
and you getClass
in return.