pair = struct.new(:token, :word)first_word = pair.new("direction", "north")
second_word = pair.new("verb", "go")
#定義乙個陣列,儲存這個新建的struct
sentence = [first_word, second_word]
p sentence
# => [#, #]
p sentence[0]
# => #
#直接使用句號操作符來顯示struct中的資料
p sentence[0].token,sentence[0].word
# "direction"
# "north"
#或者使用symbol的方式來輸出,結果相同
p sentence[0][:token],sentence[0][:word]
# "direction"
# "north"
# encoding: utf-8# t=struct.new('person',:name,:age)
#算是元程式設計?
t=struct.new('topic',:name,:replies) #topic一定得是大寫,包括類也是一樣,強制性的約定
p t # => struct::topic
t1=t.new('first',['good','not bad!','nice!'])
t2=t.new('second',['不錯','好','ok'])
topics=[t1,t2]
p topics
#中文使用p來輸出的時候顯示的是其unicode編碼
# => [#,
#]p topics[0].name
# => "first" 正是動態性的體現,只要有name就能顯示
p topics[1].replies
# => ["\u4e0d\u9519", "\u597d", "ok"]
動態建立類例項
python中要建立乙個類的例項,要首先匯入該類或者該類所屬的模組,例如 import mymodule myobject mymodule.myclass 或者from mymodule import myclass myobject myclass 如果要在程式中動態地建立類例項,也一樣要分兩步...
動態建立資料庫例項
動態建立資料庫例項 輸入引數為資料庫名 儲存的路徑名,資料庫檔名與資料庫名稱相同 usemaster godeclare dbname sysname path sysname select dbname convert varchar getdate 112 path d data ifdb id...
C 動態建立類的例項
c 動態 建立類的例項,如果對效能要求不是很高,而且需要動態 建立類的例項的時候可以考慮使用以下的方法 1.定義兩個類 基類 public class basebusiness 子類 public class samplebusiness basebusiness 2.動態 建立類的例項 程式集名稱...