为什么我们在 ruby 中使用 super 当我可以继承而不使用它时?
Why do we use super in ruby when I can inherit without its use?
好的,所以我在这里搜索了这个问题的答案,但找不到我想要的(确切地说),所以我会有点贪心,并要求一些社区时间。我希望使我的问题尽可能适用。
所以,在某些情况下,上周我一直在为 classes、class 变量和方法的想法而苦苦挣扎,但在过去的两天里,我的想法取得了重大进展理解。但是现在我面临着继承,无法弄清楚为什么在我可以继承而不使用它的情况下会使用 super 。
例如:
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)
如您所见,我已经能够在我的 Dog 和 Cat classes 上继承 Animal,我将其标记为继承,但在我的 Fish 上却无法继承,因为我没有继承。
如果有人能解释为什么我们使用 super,并指出我理解中的缺陷,我将不胜感激,我明白我可能完全只是误解了这里的用法,但我想得到它澄清。感谢您的宝贵时间,感谢您的帮助。
ty.
使用 super
可以让 class 覆盖它从其父级继承的方法并对其进行自定义。
例如,在您的示例中,Dog
和 Cat
从 Animal
继承 #initialize
- 但是如果我们想要 Dog
的一些特殊逻辑怎么办?
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
这让 Dog
自定义其初始化方法的作用,但仍会调用原始继承方法。
Excerpted from Inheritance - What is inherited?. The original author was nus. Attribution details can be found on the contributor page. The source is licenced under CC BY-SA 3.0 and may be found in the Documentation archive. Reference topic ID: 625 and example ID: 14883.
方法是继承的
class A
def boo; p 'boo' end
end
class B < A; end
b = B.new
b.boo # => 'boo'
Class方法被继承
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 B
WOO = WOO + 1
end
p B::WOO # => 2
实例变量被继承:
class A
attr_accessor :ho
def initialize
@ho = 'haha'
end
end
class B < A; end
b = B.new
p b.ho # => 'haha'
请注意,如果您在不调用 super
的情况下覆盖初始化实例变量的方法,它们将为 nil。从上面继续:
class C < A
def initialize; end
end
c = C.new
p c.ho # => nil
Class实例变量不被继承:
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 变量并不是真正的继承
它们作为 1 个变量在基础 class 和所有子class 之间共享:
class A
@@foo = 0
def initialize
@@foo += 1
p @@foo
end
end
class B < A;end
a = A.new # => 1
b = B.new # => 2
所以从上面继续:
class C < A
def initialize
@@foo = -10
p @@foo
end
end
a = C.new # => -10
b = B.new # => -9
好的,所以我在这里搜索了这个问题的答案,但找不到我想要的(确切地说),所以我会有点贪心,并要求一些社区时间。我希望使我的问题尽可能适用。
所以,在某些情况下,上周我一直在为 classes、class 变量和方法的想法而苦苦挣扎,但在过去的两天里,我的想法取得了重大进展理解。但是现在我面临着继承,无法弄清楚为什么在我可以继承而不使用它的情况下会使用 super 。
例如:
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)
如您所见,我已经能够在我的 Dog 和 Cat classes 上继承 Animal,我将其标记为继承,但在我的 Fish 上却无法继承,因为我没有继承。
如果有人能解释为什么我们使用 super,并指出我理解中的缺陷,我将不胜感激,我明白我可能完全只是误解了这里的用法,但我想得到它澄清。感谢您的宝贵时间,感谢您的帮助。
ty.
使用 super
可以让 class 覆盖它从其父级继承的方法并对其进行自定义。
例如,在您的示例中,Dog
和 Cat
从 Animal
继承 #initialize
- 但是如果我们想要 Dog
的一些特殊逻辑怎么办?
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
这让 Dog
自定义其初始化方法的作用,但仍会调用原始继承方法。
Excerpted from Inheritance - What is inherited?. The original author was nus. Attribution details can be found on the contributor page. The source is licenced under CC BY-SA 3.0 and may be found in the Documentation archive. Reference topic ID: 625 and example ID: 14883.
方法是继承的
class A
def boo; p 'boo' end
end
class B < A; end
b = B.new
b.boo # => 'boo'
Class方法被继承
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 B
WOO = WOO + 1
end
p B::WOO # => 2
实例变量被继承:
class A
attr_accessor :ho
def initialize
@ho = 'haha'
end
end
class B < A; end
b = B.new
p b.ho # => 'haha'
请注意,如果您在不调用 super
的情况下覆盖初始化实例变量的方法,它们将为 nil。从上面继续:
class C < A
def initialize; end
end
c = C.new
p c.ho # => nil
Class实例变量不被继承:
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 变量并不是真正的继承
它们作为 1 个变量在基础 class 和所有子class 之间共享:
class A
@@foo = 0
def initialize
@@foo += 1
p @@foo
end
end
class B < A;end
a = A.new # => 1
b = B.new # => 2
所以从上面继续:
class C < A
def initialize
@@foo = -10
p @@foo
end
end
a = C.new # => -10
b = B.new # => -9