2013-08-26 77 views

Trả lời

62

Nếu phương pháp có cùng tên, nghĩa là bạn đang ghi đè phương thức bạn có thể chỉ cần sử dụng super. Nếu không, bạn có thể sử dụng một alias_method hoặc một ràng buộc.

class Parent 
    def method 
    end 
end 

class Child < Parent 
    alias_method :parent_method, :method 
    def method 
     super 
    end 

    def other_method 
     parent_method 
     #OR 
     Parent.instance_method(:method).bind(self).call 
    end 
end 
+0

Điều này không hiệu quả. 'Child.new.other_method' trả về' NoMethodError: super: không có phương thức superclass 'other_method' cho # '. Lệnh gọi 'super.method()' sẽ chỉ gọi 'phương thức' trên bất kỳ thứ gì được trả về từ lệnh gọi' super' (trong trường hợp này, không tồn tại). 'super' không phải là tham chiếu đến' superclass' của một cá thể. – Cade

+0

@Xin bạn hoàn toàn chính xác. Tôi đã thực hiện chỉnh sửa cho số này –

+1

Cũng gọi 'super' sẽ gọi siêu với các đối số chính xác đã được chuyển cho phương thức con. Nếu bạn muốn chỉ định các đối số, bạn có thể gọi super với chúng: EG: 'def child (a, b); siêu; end' vs 'def child (a, b); siêu (somevar); end' – Adam

11

Từ khóa thực sự gọi phương thức cùng tên trong lớp cha mẹ. (source)

class Foo 
    def foo 
    # Do something 
    end 
end 

class Bar < Foo 
    def foo 
    super # Calls foo() method in parent class 
    end 
end 
+11

Đặt một ví dụ trong Java không phải là ý tưởng tốt nhất nếu người yêu cầu đề cập rõ ràng PHP như ngôn ngữ khác mà anh ta biết ... –

+1

câu trả lời này hữu ích cho tôi, và anh ta nói anh ta biết PHP và các ngôn ngữ khác có tính năng này (không phải anh ta chỉ biết PHP). –

15

Các super keyword gọi một phương thức cùng tên trong lớp siêu:

class Foo 
    def foo 
    "#{self.class}#foo" 
    end 
end 

class Bar < Foo 
    def foo 
    "Super says: #{super}" 
    end 
end 

Foo.new.foo # => "Foo#foo" 
Bar.new.foo # => "Super says: Bar#foo" 
5

Những người khác đã nói nó đã tốt. Chỉ cần lưu ý thêm một lần nữa:

Cú pháp super.foo để gọi phương thức foo trong lớp siêu là không được hỗ trợ. Thay vào đó, nó sẽ gọi phương thức super và trên kết quả trả về, hãy thử gọi foo.

class A 
    def a 
     "A::a" 
    end 
    end 

    class B < A 
    def a 
     "B::a is calling #{super.a}" # -> undefined method `a` for StringClass 
    end 
    end 
+0

Đây là một sự khác biệt quan trọng mà mọi người nên biết, vì vậy cảm ơn bạn đã chỉ ra điều này! – Magne

0
class Parent 
    def self.parent_method 
    "#{self} called parent method" 
    end 

    def parent_method 
    "#{self} called parent method" 
    end 
end 

class Child < Parent 

    def parent_method 
    # call parent_method as 
    Parent.parent_method    # self.parent_method gets invoked 
    # call parent_method as 
    self.class.superclass.parent_method # self.parent_method gets invoked 
    super        # parent_method gets invoked 
    "#{self} called parent method"  # returns "#<Child:0x00556c435773f8> called parent method" 
    end 

end 

Child.new.parent_method #This will produce following output 
Parent called parent method 
Parent called parent method 
#<Child:0x00556c435773f8> called parent method 
#=> "#<Child:0x00556c435773f8> called parent method" 

self.class.superclass == Parent #=> true

Parent.parent_methodself.class.superclass sẽ gọi self.parent_method (phương pháp lớp) của Parent khi super gọi (phương pháp chẳng hạn) parent_method của Parent.

Các vấn đề liên quan