有人可以解释Class.superclass.class.superclass悖论吗? [英] Can someone explain the Class.superclass.class.superclass paradox?

查看:115
本文介绍了有人可以解释Class.superclass.class.superclass悖论吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能根本不是悖论,但从新手的角度来看,肯定是这样.

It's probably not a paradox at all, but from a newbies perspective, it sure seems that way.

> Class.superclass
=> Module
> Class.superclass.class
=> Class
> Class.superclass.class.superclass
=> Module

因此,类的父级是模块,而模块是类?

So a class's parent is module, but module is a class?

我该如何理解?

推荐答案

TL; DR:模块是 Class的超类. Module是 Class的实例.


让我尝试更清楚地解释它.请原谅我的手写绘图-我没有任何精美的绘图软件.

TL;DR: Module is the superclass of Class. Module is an instance of Class.


Let me try to explain it more clearly. Please forgive my handwritten drawings - I don't have any fancy drawing software.

Ruby中的每个类都有1个超类*.

Every class in Ruby has 1 superclass*.

*除了BasicObject之外,它没有超类.

*Except for BasicObject, which doesn't have a superclass.

像这样阅读上面的图形:Float的超类是数字.数字的超类是对象,等等.

Read the above graphic like this: The superclass of Float is Numeric. The superclass of Numeric is Object, etc...

实例化一个对象时,该对象将是某个类的实例.例如,"Nathan"是String类的实例. 乔"或约翰"也是如此. 1是Fixnum类的实例,2、3、4等...

When you instantiate an object, the object will be an instance of some class. For example, "Nathan" is an instance of the String class. So is "Joe" or "John". 1 is an instance of the Fixnum class, as are 2, 3, 4, etc...

这样阅读上面的图形:"Joe"是String的一个实例. 1是Fixnum的实例,等等...

Read the above graphic like this: "Joe" is an instance of String. 1 is an instance of Fixnum, etc...

好吧,在Ruby中,与大多数其他语言不同,Class是另一个类,它也可以实例化,就像Fixnum或String一样.

Well, in Ruby, unlike in most other languages, Class is a just another class, and it can be instantiated, too, just like Fixnum or String.

这样阅读上面的图形:0.01是Float的一个实例.字符串是Class的实例,等等.

Read the above graphic like this: 0.01 is an instance of Float. String is an instance of Class, etc...

意识到Fixnum是Class的实例,就像"Nathan"是String的实例一样.就像"John"是String的实例一样,Float只是Class的实例.每个类都只是类的一个实例,甚至是类本身!

Realize that Fixnum is an instance of Class, just like "Nathan" is an instance of String. Just like "John" is an instance of String, Float is just an instance of Class. Every class is just an an instance of Class, even Class itself!

每当在应用程序中编写一个新类时,您都将实例化一个类为Class的新对象,就像Hash一样.new实例化一个新的Hash,或者"Nathan"实例化一个新的String.

Whenever you write a new class in your app, you are just instantiating a new object whose class is Class, just like Hash.new instantiates a new Hash, or "Nathan" instantiates a new String.

# By running this, you will be instantiating a new Class, and 
# it will be named Post 
class Post < ActiveRecord::Base
end

# Here is another perfectly valid way to write the above code:
Post = Class.new(ActiveRecord::Base)

# you can even instantiate a Class without giving it an explicit name:
x = Class.new(ActiveRecord::Base)

# and since your new things are classes, they can be instantiated
obj1 = Post.new
obj2 = x.new

此外,Module只是Class的另一个实例.每当您在应用中编写新模块时,您都只是在实例化一个新模块.

Furthermore, Module is just another instance of Class. Whenever you write a new module in your app, you are just instantiating a new Module.

# this will instantiate a new Module, and assign it to Foo
module Foo
end

# Here is another perfectly valid way to write the above code:
Foo = Module.new

# you can even instantiate a Module without giving it an explicit name.
m = Module.new

顺便说一句:模块只是方法和常量的集合.类也是方法和常量的集合,但是具有能够实例化的附加功能.无法实例化模块.也就是说,m.new将不起作用.

An aside: A Module is just a collection of methods and constants. Classes are also a collection of methods and constants, but with the added functionality of being able to be instantiated. A module cannot be instantiated. That is, m.new will not work.

因此,请回到顶部图形,可以直接回答您的问题:

So, referring back to the top graphic, your question can be answered directly:

因此,类的父级是模块,而模块是类?

So a class's parent is module, but module is a class?

您可以从顶部图形中看到:模块是 Class的超类.

You can see from the top graphic: Module is the superclass of Class.

在底部的图形中:Module是 Class的实例.

From the bottom graphic: Module is an instance of Class.

这篇关于有人可以解释Class.superclass.class.superclass悖论吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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