是否可以在不影響其他子類的情況下,在基類內的子類內動態創建/設置變量? 例如,這裏藉此代碼: class Base:
@classmethod
def __init__(cls,v):
cls.v=v
class sub1(Base):
Base.__init__(1)
class sub2(Base):
Base.__init__(5)
我想拒絕在Ruby中創建實例變量,以防止無意的變量被錯誤地創建。 My class:
class Test
def initialize
@a = 'Var A'
end
def make_new
@b = 'Var B' <-- I would like to deny creation of any variables that were
類變量是這樣的: class Hello
@@x = 0
def self.counter
@@x
end
def initialize
@@x += 1
end
end
Hello.new
Hello.new
Hello.new
p Hello.counter
#=> 3
但類實例變量不: class G
我試圖創建一個化學GUI,顯示有關每個元素的各種信息。我正在使用類實例列表來打印出這些信息,但我仍然得到一個'list' object has no attribute 'atomic_number'。這是我建立的班級,以及給我錯誤的代碼。 class ElementInformation(object):
def __init__(self, atomic_number, elemen
我需要一個不會繼承的類變量,所以我決定使用一個類實例變量。目前我有這樣的代碼: class A
def self.symbols
history_symbols
end
private
def self.history_tables
@@history_tables ||= ActiveRecord::Base.connection.