为什么我们在ruby中使用super,而我可以不使用它来继承?
好的,我在这里搜索了这个问题的答案,但找不到我想要的(确切地说),所以我会有点贪婪,并要求一些时间。我希望我的问题尽可能适用 所以,在某些情况下,我在上周一直在为类、类变量和方法的概念而挣扎,但在过去的两天里,我的理解取得了重大进展。然而,现在我面临着继承问题,我不明白为什么会使用super,而我可以直接继承而不使用它 例如:为什么我们在ruby中使用super,而我可以不使用它来继承?,ruby,class,inheritance,initialization,super,Ruby,Class,Inheritance,Initialization,Super,好的,我在这里搜索了这个问题的答案,但找不到我想要的(确切地说),所以我会有点贪婪,并要求一些时间。我希望我的问题尽可能适用 所以,在某些情况下,我在上周一直在为类、类变量和方法的概念而挣扎,但在过去的两天里,我的理解取得了重大进展。然而,现在我面临着继承问题,我不明白为什么会使用super,而我可以直接继承而不使用它 例如: class Animal def initialize (type, breed, age) @type = type @breed = br
class Animal
def initialize (type, breed, age)
@type = type
@breed = breed
@age = age
end
end
class Dog < Animal
end
class Cat < Animal
end
class Fish
end
woof = Dog.new("dog", "shitzu", 12)
meow = Cat.new("cat", "tabby", 5)
fish = Fish.new("fish", "gold", 2)
Output:
=> #<Dog:0x00000001447680 @type="dog", @breed="shitzu", @age=12>
=> #<Cat:0x0000000140c918 @type="cat", @breed="tabby", @age=5>
ArgumentError: wrong number of arguments (given 3, expected 0)
类动物
def初始化(类型、品种、年龄)
@类型=类型
@品种
@年龄=年龄
结束
结束
类狗<动物
结束
猫类<动物类
结束
分类鱼
结束
woof=狗。新(“狗”,“狮子”,12)
喵喵=猫。新(“猫”,“斑猫”,5)
鱼=鱼。新(“鱼”,“金”,2)
输出:
=> #
=> #
ArgumentError:参数数目错误(给定3,应为0)
正如你们所看到的,我已经能够在我的狗和猫类上继承动物,我标记为继承,但在我的鱼上我不能,因为我没有继承
如果有人能解释为什么我们使用super,并指出我理解中的缺陷,我将非常感激,我理解我可能完全误解了这里的用法,但我想澄清一下。谢谢你的时间,谢谢你的帮助
ty.使用
super
可以让类重写从其父类继承的方法并对其进行自定义
例如,在您的示例中,狗
和猫
继承#初始化来自动物
——但是如果我们想要狗
的一些特殊逻辑呢
class Dog < Animal
def initialize(type, breed, age)
raise "Sorry, dogs don't live that long!" if age > 100
# Everything looks good - let Animal#initialize run now
super
end
end
class狗
这允许Dog
自定义其initialize方法的功能,但仍然调用原始继承的方法。使用super
可以让类重写从其父类继承的方法并对其进行自定义
例如,在您的示例中,狗
和猫
继承#初始化来自动物
——但是如果我们想要狗
的一些特殊逻辑呢
class Dog < Animal
def initialize(type, breed, age)
raise "Sorry, dogs don't live that long!" if age > 100
# Everything looks good - let Animal#initialize run now
super
end
end
class狗
这允许Dog
自定义其initialize方法的功能,但仍然调用原始继承的方法
摘自。原作者是。有关归属的详细信息,请访问。该来源已根据获得许可,可在中找到。参考主题ID:625和示例ID:14883
方法是继承的
class A
def boo; p 'boo' end
end
class B < A; end
b = B.new
b.boo # => 'boo'
class A
def self.boo; p 'boo' end
end
class B < A; end
p B.boo # => 'boo'
class A
WOO = 1
end
class B < A; end
p B::WOO # => 1
实例变量被继承:
class A
attr_accessor :ho
def initialize
@ho = 'haha'
end
end
class B < A; end
b = B.new
p b.ho # => 'haha'
class A
@foo = 'foo'
class << self
attr_accessor :foo
end
end
class B < A; end
p B.foo # => nil
# The accessor is inherited, since it is a class method
#
B.foo = 'fob' # possible
类实例变量不被继承:
class A
attr_accessor :ho
def initialize
@ho = 'haha'
end
end
class B < A; end
b = B.new
p b.ho # => 'haha'
class A
@foo = 'foo'
class << self
attr_accessor :foo
end
end
class B < A; end
p B.foo # => nil
# The accessor is inherited, since it is a class method
#
B.foo = 'fob' # possible
A类
@foo='foo'
零级
#访问器是继承的,因为它是类方法
#
B.foo=‘fob’#可能
类变量不是真正继承的
class A
def boo; p 'boo' end
end
class B < A; end
b = B.new
b.boo # => 'boo'
class A
def self.boo; p 'boo' end
end
class B < A; end
p B.boo # => 'boo'
class A
WOO = 1
end
class B < A; end
p B::WOO # => 1
它们作为一个变量在基类和所有子类之间共享:
class A
@@foo = 0
def initialize
@@foo += 1
p @@foo
end
end
class B < A;end
a = A.new # => 1
b = B.new # => 2
A类
@@foo=0
def初始化
@@foo+=1
p@@foo
结束
结束
B级1
b=b.新的#=>2
因此,从上面继续:
class C < A
def initialize; end
end
c = C.new
p c.ho # => nil
class C < A
def initialize
@@foo = -10
p @@foo
end
end
a = C.new # => -10
b = B.new # => -9
C类-10
b=b.new#=>-9
摘自。原作者是。有关归属的详细信息,请访问。该来源已根据获得许可,可在中找到。参考主题ID:625和示例ID:14883
方法是继承的
class A
def boo; p 'boo' end
end
class B < A; end
b = B.new
b.boo # => 'boo'
class A
def self.boo; p 'boo' end
end
class B < A; end
p B.boo # => 'boo'
class A
WOO = 1
end
class B < A; end
p B::WOO # => 1
实例变量被继承:
class A
attr_accessor :ho
def initialize
@ho = 'haha'
end
end
class B < A; end
b = B.new
p b.ho # => 'haha'
class A
@foo = 'foo'
class << self
attr_accessor :foo
end
end
class B < A; end
p B.foo # => nil
# The accessor is inherited, since it is a class method
#
B.foo = 'fob' # possible
类实例变量不被继承:
class A
attr_accessor :ho
def initialize
@ho = 'haha'
end
end
class B < A; end
b = B.new
p b.ho # => 'haha'
class A
@foo = 'foo'
class << self
attr_accessor :foo
end
end
class B < A; end
p B.foo # => nil
# The accessor is inherited, since it is a class method
#
B.foo = 'fob' # possible
A类
@foo='foo'
零级
#访问器是继承的,因为它是类方法
#
B.foo=‘fob’#可能
类变量不是真正继承的
class A
def boo; p 'boo' end
end
class B < A; end
b = B.new
b.boo # => 'boo'
class A
def self.boo; p 'boo' end
end
class B < A; end
p B.boo # => 'boo'
class A
WOO = 1
end
class B < A; end
p B::WOO # => 1
它们作为一个变量在基类和所有子类之间共享:
class A
@@foo = 0
def initialize
@@foo += 1
p @@foo
end
end
class B < A;end
a = A.new # => 1
b = B.new # => 2
A类
@@foo=0
def初始化
@@foo+=1
p@@foo
结束
结束
B级1
b=b.新的#=>2
因此,从上面继续:
class C < A
def initialize; end
end
c = C.new
p c.ho # => nil
class C < A
def initialize
@@foo = -10
p @@foo
end
end
a = C.new # => -10
b = B.new # => -9
C类-10
b=b.new#=>-9
您是否想知道对super
的显式调用?(比如,当一个方法调用“super”来运行其父类的方法版本时?)如果您想自定义classDog
中的initialize
方法,那么简单地从classAnimal
继承将不允许您自定义它。如果在类Dog
中,您执行了类似def initialize(type,breed,age,color)
的操作,然后使用super(type,breed,age)
和@color=color
,这将允许您更改在调用其父类Animal
时继承的initialize方法,以初始化您在示例中使用的其他实例变量super
?我不明白这个问题与代码示例有什么关系。@gmcnaughton和philip yoo(很抱歉,我不能在同一条评论中标记这两个)解释了这一点。对不起,我问了个愚蠢的问题,我刚上了几门不同的在线课程,似乎到处都有添加,但我不知道应该是什么时候used@JoshuaCheek好吧,这是我的一半问题,公平地说,我不知道,我有点想问super是如何应用的,我认为它与initialize方法有关。你想知道显式调用吗