班级声明是红宝石的洗眼液吗?一切都是面向对象的吗? [英] Is Class declaration an eyewash in ruby? Is everything really object oriented?

查看:50
本文介绍了班级声明是红宝石的洗眼液吗?一切都是面向对象的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Person
  def name
   puts "Dave"
  end
end

puts Person.object_id

访问方法只有两种方式:

There are only two ways of accessing methods :

1) Someclass.method 在类方法的情况下.#where Someclass 是一个类.

1) Someclass.method in case of class methods. #where Someclass is a class.

2) 和 Object.method 当被访问的方法是在类中声明的常规方法时.而 Object 是一个类的实例.

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.

它遵循模式 Object.method 那么,这是否意味着 Person 类真的是一个对象?

It follows the pattern Object.method so, does it mean Person class is really an object?

或者 object_id 是一个类方法?后者似乎不太可能,因为类方法不能继承到实例中.但是当我们做这样的事情时:

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 是 Person 类的实例,因此 object_id 不能是类方法.

a is an instance of Person class so object_id cannot be a class method.

推荐答案

是的,Ruby 类是对象:

Yes, Ruby classes are objects:

>> String.is_a? Object
=> true
>> String.methods.count
=> 131
>> Fixnum.methods.count
=> 128

这篇关于班级声明是红宝石的洗眼液吗?一切都是面向对象的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆