2
我在我的基類中有一個方法,我需要從我的派生類調用此方法。是否有可能使用靜態方法?從派生類方法調用基類方法
class base < A
def self.method1
end
end
class derived < base
def method2
base.method1
end
end
這樣可能嗎?這是對的嗎?
我在我的基類中有一個方法,我需要從我的派生類調用此方法。是否有可能使用靜態方法?從派生類方法調用基類方法
class base < A
def self.method1
end
end
class derived < base
def method2
base.method1
end
end
這樣可能嗎?這是對的嗎?
是..
class Base
def self.method1
p "hi"
end
end
class Derived < Base
def method2
self.class.method1
end
end
Derived.new.method2
# >> "hi"
問題(輕度無關):我不認爲你可以通過靜態方法調用靜態方法嗎?在這個例子中,如果它是'self.method2',那麼我認爲會出現這樣的錯誤:'Class:Class'的undefined method'method1'。 – Sticky
在Ruby中的類名或模塊名稱始終以大寫字母開始,因爲類名和模塊名是常數。 – r92