[问题] Access derived class from base class

楼主: ScottOAO (cos)   2015-05-20 08:12:36
假设今天有
class creature
class animal : creature
class plants : creature
然后阵列可能存animal 或plants
creature[] c;
c[0] = new animal();
c[1] = new plants();
但是我要如何判断阵列中存的是何种derived class?
有没有不用dynamic的做法? 谢谢
补问一下,强制cast好吗?
作者: ssccg (23)   2015-05-20 09:06:00
c[0].getType()要判断的话用 C[0] is Animal、c[0] is Plants
作者: Litfal (Litfal)   2015-05-20 09:19:00
先想想能不能用多型解决

Links booklink

Contact Us: admin [ a t ] ucptt.com