Cả hai đều gọi method cùng tên ở superclass, nhưng khác nhau về argument:
super | super() | |
|---|---|---|
| Argument truyền lên | Tự động forward tất cả arg của method hiện tại | Gọi superclass method không có argument |
ruby
class Animal
def initialize(name, age)
@name, @age = name, age
end
end
class Dog < Animal
def initialize(name, age, breed)
super(name, age) # tường minh — chỉ truyền 2 arg
@breed = breed
end
end
class Cat < Animal
def initialize(name, age)
super # forward cả name + age
end
end
class Bird < Animal
def initialize(name)
super() # gọi Animal#initialize không arg → ArgumentError sẽ xảy ra nếu parent yêu cầu arg
end
endLưu ý: dùng super không tường minh khi signature parent và child giống nhau; dùng super(arg1, arg2) hoặc super() khi cần kiểm soát arg.
Both call the same-named method in the superclass, but differ in argument forwarding:
super | super() | |
|---|---|---|
| Arguments passed | Auto-forwards all current method args | Calls parent with no arguments |
ruby
class Animal
def initialize(name, age)
@name, @age = name, age
end
end
class Dog < Animal
def initialize(name, age, breed)
super(name, age) # explicit — only 2 args
@breed = breed
end
end
class Cat < Animal
def initialize(name, age)
super # forwards both name + age
end
endRule of thumb: use bare super when child and parent signatures match; use super(args) or super() when you need to control what gets forwarded.