「Java」instanceof演算子でオブジェクトの比較サンプルコード
Javaコード
Station s = new Station();
Cell c = new Cell();
List list = new ArrayList();
list.add(s);
list.add(c);
Iterator it = list.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof Station ) {
Station s1 = (Station ) obj;
s1.stationDo();
}
if (obj instanceof Cell ) {
Cell c1 = (Cell ) obj;
c1.cellDo();
}
}