Java知识点:instanceof关键字
介绍
- 格式:a instanceof B,其中a是一个variable(设a所指向的对象的类型命名为A,即A是一个type。
- 功能:判断A是否 is-a B。
- 当a=null时,始终返回false。
- 当B=Object时,始终返回true。
可能返回的结果
- true:A is-a B。
- false:
- B是一个类时,A和B在一棵继承树,但是A is-not-a B。
- B是一个接口时,A不是final的,A is-not-a B(不要求A和B要在一棵继承树上)。
- Compile Error
- B是一个类时,A和B不在一棵继承树上,则Compile Error。
- B是一个接口时,如果A是final的且A和B不在一棵继承树上,则Compile Error。
实验一:接口的情况
interface A{}
class B implements A{}
class C{}
final class D{}
class Demo01
{
public static void main(String args[])
{
B b = new B();
C c = new C();
D d = new D();
System.out.println(b instanceof A); //true
System.out.println(c instanceof A); //false
System.out.println(d instanceof A); //Compile Error:不可转换的类型
}
}
实验二:类的情况
public class Instanceof01
{
public static void main(String[] args) {
A a = new A();
A b = new B();
System.out.println((a instanceof B)); // A isn't-a B,因此输出false
System.out.println((b instanceof A)); // B is-a A,因此输出true
System.out.println((b instanceof B)); // B is-a B,因此输出true
System.out.println((a instanceof C)); // Compile Error:不可转换的类型,因为类A和类C不在一棵继承树上
}
}
class A{}
class B extends A{}
class C{}
Java知识点:instanceof关键字的更多相关文章
- Java-Runoob-高级教程-实例-方法:07. Java 实例 – instanceOf 关键字用法
ylbtech-Java-Runoob-高级教程-实例-方法:07. Java 实例 – instanceOf 关键字用法 1.返回顶部 1. Java 实例 - instanceof 关键字用法 ...
- Java 实例 - instanceof 关键字用法
Java 实例 - instanceof 关键字用法 instanceof 是 Java 的一个二元操作符,类似于 ==,>,< 等操作符. instanceof 是 Java 的保留关键 ...
- Java中instanceof关键字的用法
Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. instanc ...
- JAVA面向对象-----instanceof 关键字
instanceof 关键字 1:快速演示instanceof Person p=new Person(); System.out.println( p instanceof Person); 2:i ...
- java的instanceof关键字
java 中的instanceof 运算符是用来判断对象是否是 特定类或这个特定类的子类 的一个实例. 用法: result = object instanceof class 参数: Result: ...
- Java中instanceof关键字的理解
java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法: res ...
- Java 基础 - instanceof关键字
instanceof 父类子类 结论: 子类 instanceof 父类 == true 父类 instanceof 子类 == false public class Test { public st ...
- JAVA中INSTANCEOF关键字的用法总结
https://www.cnblogs.com/jay36/p/7519145.html https://www.cnblogs.com/zjxynq/p/5882756.html https://b ...
- Java instanceof 关键字是如何实现的?
作者:RednaxelaFX链接:https://www.zhihu.com/question/21574535/answer/18998914来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...
- Java中的instanceof关键字
instanceof是Java的一个二元操作符,和==,>,<是同一类东东.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是测试它左边的对象是否是它右边的类的实例,返回boo ...
随机推荐
- 3242: [Noi2013]快餐店 - BZOJ
Description 小T打算在城市C开设一家外送快餐店.送餐到某一个地点的时间与外卖店到该地点之间最短路径长度是成正比的,小T希望快餐店的地址选在离最远的顾客距离最近的地方. 快餐店的顾客分布在城 ...
- EXT--columnWidth
在EXT 3.4API上没有查询到columnWidth这个配置项,但它却实实在在的在起作用,后来在ColumnLayout类查到它的信息: 上面的信息描述了采用了columnLayout布局的子面板 ...
- Codeforces Round #240 (Div. 2)->A. Mashmokh and Lights
A. Mashmokh and Lights time limit per test 1 second memory limit per test 256 megabytes input standa ...
- linux源代码阅读笔记 free_page_tables()分析
/* 77 * This function frees a continuos block of page tables, as needed 78 * by 'exit()'. As does co ...
- Observer Pattern
Motivation We can not talk about Object Oriented Programming without considering the state of the ob ...
- @JsonFormat时间不对
实际时间为:2015-07-06 20:20:23 1. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date ...
- lintcode: 二叉查找树中搜索区间
题目 二叉查找树中搜索区间 给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点.找到树中所有值在 k1 到 k2 范围内的节点.即打印所有x (k1 <= x <= ...
- iOS Objective-C对象模型及应用
前言 原创文章,转载请注明出自唐巧的技术博客. 本文主要介绍Objective-C对象模型的实现细节,以及Objective-C语言对象模型中对isa swizzling和method swizzli ...
- HTTP长连接实现“服务器推”的技术
HTTP长连接实现“服务器推”的技术快速入门及演示示例 在我的印象里HTTP是一种“无状态的协议”,也就是不知道以前请求的历史,无法保留上一次请求的结果.Cookie的诞生,弥补了这个不足,浏览器可以 ...
- js中鼠标滚轮事件详解
js中鼠标滚轮事件详解 (以下内容部分内容参考了http://adomas.org/javascript-mouse-wheel/ ) 之前js 仿Photoshop鼠标滚轮控制输入框取值中已使用 ...