Arts >> Arte e intrattenimento >  >> Magia >> Trucchi di magia

Usare `super` in ereditarietà Ruby: Chiamare il metodo genitore

In Ruby (il linguaggio), super è una keyword che chiama il metodo di una classe genitore. Non c'entra nulla con l'oggetto Pokémon "Super Rod".

Come Funziona `super`

class Vehicle def initialize(brand) @brand = brand end def start puts "Starting #{@brand} vehicle..." end end class Car < Vehicle def start super # calls Vehicle#start puts "Engine started. Ready to drive!" end end Car.new("Toyota").start # Output: # Starting Toyota vehicle... # Engine started. Ready to drive! 

Punti Chiave

Questo è comportamento standard single-inheritance in Ruby.

Trucchi di magia

Related Categories