看板 C_Sharp 關於我們 聯絡資訊
假設今天有 class creature class animal : creature class plants : creature 然後陣列可能存animal 或plants creature[] c; c[0] = new animal(); c[1] = new plants(); 但是我要如何判斷陣列中存的是何種derived class? 有沒有不用dynamic的做法? 謝謝 補問一下,強制cast好嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.167.159.126 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1432080759.A.B46.html
ssccg: c[0].getType() 05/20 09:06
對耶謝謝:P
ssccg: 要判斷的話用 C[0] is Animal、c[0] is Plants 05/20 09:09
Litfal: 先想想能不能用多型解決 05/20 09:19
正是因為不是多型才麻煩阿~~ ※ 編輯: ScottOAO (118.167.159.126), 05/20/2015 09:47:15 ※ 編輯: ScottOAO (118.167.159.126), 05/20/2015 09:52:33
Litfal: 這種情況很常發生的話,我會在creature裡面加屬性就是了 05/23 14:21