跟进一个较早的 SO 问题,现在我想将其收集Fruit
到一个篮子中,但要知道出路时的子类型。
class Banana : Fruit {
var color: string;
}
class Apple: Fruit {
var poison: bool;
}
class Fruit {
}
var a = new Apple(poison=true);
var b = new Banana(color="green");
if (a.type == Apple) {
writeln("Go away doctor!");
}
var basketDom = {1..1};
var basket: [basketDom] Fruit;
basket.push_back(a);
basket.push_back(b);
for b in basket {
writeln(b.type:string);
}
这将打印超类型Fruit
。我怎样才能Apples
进出Bananas
这个篮子?