[英]How do I get the parent's class name in Ruby
Let assume I have a classes A
and B
where B
inherits A
. 假设我有一个
A
和B
类,其中B
继承A
How do I print parent class name in B
如何在
B
打印父类名称
class A
end
class B < A
end
Some things I have tried 我试过的一些事情
>> B.new.class #=> B #which is correct
>> B.new.parent #=> Undefined method `parent`
>> B.parent #=> Object
>> B.parent.class #=> Class
Thanks :) 谢谢 :)
class A
end
class B < A
end
B.superclass # => A
B.superclass.name # => "A"
If you want the full ancestor stack try: 如果你想要完整的祖先堆栈尝试:
object.class.ancestors
For instance: 例如:
> a = Array.new
=> []
> a.class.ancestors
=> [Array, Enumerable, Object, Kernel, BasicObject]
Given an object (Instantiated Class) you can derive the parent Class 给定一个对象(实例化类),您可以派生父类
>> x = B.new
>> x.class.superclass.name
=>"A"
In case google brings anyone here who's working in Rails, what you may want instead is base_class
, as superclass
will traverse the ActiveRecord inheritance structure as well. 如果谷歌带来了任何在Rails工作的人,你可能需要的是
base_class
,因为superclass
也将遍历ActiveRecord继承结构。
class A < ActiveRecord::Base
end
class B < A
end
> A.superclass
=> ActiveRecord::Base
> B.superclass
=> A
> A.base_class
=> A
> B.base_class
=> A
Even further... 更深入...
class C < B
end
> C.base_class
=> A
In other words, base_class
gives you the top of the inheritance tree but limited to the context of your application. 换句话说,
base_class
为您提供继承树的顶部,但仅限于应用程序的上下文。 Fair warning though, as far as Rails is concerned "your application" includes any gems you're using, so if you have a model that subclasses something defined in a gem, base_class
will return the gem's class, not yours. 但是公平的警告,就Rails而言,“你的应用程序”包含你正在使用的任何宝石,所以如果你有一个
base_class
宝石中定义的东西的模型, base_class
将返回gem的类,而不是你的。
The term you're looking for is superclass
. 您正在寻找的术语是
superclass
。 And indeed you can do B.superclass
to get A
. 事实上,你可以做
B.superclass
来获得A
(You can also do B.ancestors
to get a list of all the classes and modules it inherits from — something like [B, A, Object, Kernel, BasicObject]
.) (你也可以做
B.ancestors
来获取它继承的所有类和模块的列表 - 比如[B, A, Object, Kernel, BasicObject]
。)
Inheritance is a relation between two classes.
继承是两个类之间的关系。 Inheritance create a parent child relationship between classes.
继承在类之间创建父子关系。 It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces.The benefit of inheritance is that classes lower down the hierarchy get the features of those higher up, but can also add specific features of their own.
它是一种代码重用的机制,允许通过公共类和接口对原始软件进行独立扩展。继承的好处是层次结构较低的类可以获得更高级别的功能,但也可以添加自己的特定功能。
In Ruby, a class can only inherit from a single other class. 在Ruby中,类只能从一个其他类继承。 (ie a class can inherit from a class that inherits from another class which inherits from another class, but a single class can not inherit from many classes at once).
(即,一个类可以继承自另一个继承自另一个类的类的类,但是单个类不能同时从多个类继承)。 The BasicObject class is the parent class of all classes in Ruby.
BasicObject类是Ruby中所有类的父类。 Its methods are therefore available to all objects unless explicitly overridden.
因此,除非明确覆盖,否则其方法可用于所有对象。
Ruby overcome the single class inheritance at once by using the mixin.
Ruby使用mixin立即克服了单类继承。
I will try to explain with an example. 我将尝试用一个例子来解释。
module Mux
def sam
p "I am an module"
end
end
class A
include Mux
end
class B < A
end
class C < B
end
class D < A
end
You can trace by using class_name.superclass.name and do this process unless you found BasicOject in this hierarchy. 您可以使用class_name.superclass.name进行跟踪并执行此过程,除非您在此层次结构中找到BasicOject。 BasicObject is super class o every classes.
BasicObject是每个类的超类。 lets see suppose we want to see class C hierarchy tree.
让我们看看我们想要看到C类层次结构树。
C.superclass
=> B
B.superclass
=> A
A.superclass
=> Object
Object.superclass
=> BasicObject
You can see the whole hierarchy of class C. Point to note using this approach you will not find modules that are included or prepended in the parent classes. 您可以看到C类的整个层次结构。使用此方法注意,您将找不到包含在父类中或预先包含在父类中的模块。
There is another approach to find complete hierarchy including modules. 还有另一种方法可以找到包括模块的完整层次结构 According to Ruby doc ancestors .
据Ruby doc 祖先说 。 Returns a list of modules included/prepended in mod (including mod itself).
返回包含/预先包含在mod中的模块列表(包括mod本身)。
C.ancestors
=> [C, B, A, Mux, Object, Kernel, BasicObject]
Here, Mux and Kernel are Modules. 这里,Mux和Kernel是模块。
http://rubylearning.com/satishtalim/ruby_inheritance.html https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) http://rubylearning.com/satishtalim/ruby_inheritance.html https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.