instanceof 的语法格式如下:
myobject instanceof ExampleClass
myobject:某类的对象引用
ExampleClass:某个类
class Quadrangle{
public static void draw(Quadrangle q) {
}
} class Square extends Quadrangle { } class Anything { }
public class Parallelogram extends Quadrangle { public static void main(String[] args) { Quadrangle q = new Quadrangle();
//判断父类对象是否为Parallelogram子类的一个实例
if(q instanceof Parallelogram) {
Parallelogram p = (Parallelogram) q;
}
if(q instanceof Square){
Square s = (Square)q;
}
} }