instanceof是二元运算符,左边是对象,右边是类;当对象是右面类或子类所创建对象时,返回true;否则,返回false。比如: 【示例5-2】使用instanceof运算符进行类型判断 [url=][/url]
public class User2 { public static void main(String[] args) { Student student = new Student("郭少", 176, "安全"); System.out.println(student instanceof Student); System.out.println(student instanceof Person); System.out.println(student instanceof Object); System.out.println(new Person() instanceof Student); System.out.println(new Person() instanceof Object); }}[url=][/url]
结果如下:
|