桌面支持--outlook2010邮件召回
2015-08-19 16:47
387 查看
Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics):
The Type Comparison Operator instanceof
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
The following program, InstanceofDemo, defines a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface.
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println('obj1 instanceof Parent: ' + (obj1 instanceof Parent));
System.out.println('obj1 instanceof Child: ' + (obj1 instanceof Child));
System.out.println('obj1 instanceof MyInterface: ' + (obj1 instanceof MyInterface));
System.out.println('obj2 instanceof Parent: ' + (obj2 instanceof Parent));
System.out.println('obj2 instanceof Child: ' + (obj2 instanceof Child));
System.out.println('obj2 instanceof MyInterface: ' + (obj2 instanceof MyInterface));
The Type Comparison Operator instanceof
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
The following program, InstanceofDemo, defines a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface.
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println('obj1 instanceof Parent: ' + (obj1 instanceof Parent));
System.out.println('obj1 instanceof Child: ' + (obj1 instanceof Child));
System.out.println('obj1 instanceof MyInterface: ' + (obj1 instanceof MyInterface));
System.out.println('obj2 instanceof Parent: ' + (obj2 instanceof Parent));
System.out.println('obj2 instanceof Child: ' + (obj2 instanceof Child));
System.out.println('obj2 instanceof MyInterface: ' + (obj2 instanceof MyInterface));
} } class Parent{} class Child extends Parent implements MyInterface{} interface MyInterface{}Output:
obj1 instanceof Parent: true obj1 instanceof Child: false obj1 instanceof MyInterface: false obj2 instanceof Parent: true obj2 instanceof Child: true obj2 instanceof MyInterface: trueWhen using the
instanceofoperator, keep in mind that
nullis not an instance of anything.
相关文章推荐
- css 超链接 汉字不自动换行 white-space:nowrap;
- Linux系统日常管理
- mac 上wget安装
- aauto学习系列之<13>库
- Xcode 管理provisioning profile
- /proc/dev/net
- javascript函数对象模式--纠结的用法
- 在SpringMVC中获取request对象的几种方式
- 一文让你彻底了解iOS字体相关知识
- ext.net 后台方法调用
- 安卓去掉标题栏
- android布局------RelativeLayout(相对布局)详解
- iOS动画——DynamicAnimate
- mysql基本内容学习过程
- 使用c++代替使用的c包!
- 桌面支持--outlook 2010初始化
- 桌面支持--office365 pop3设置
- ajax提交异步,同步请求不同
- [Win32] DLL注入技术
- Redis集群进阶-移除节点