- instanceof 和 isInstance 强转 类型 class MD
| 我的GitHub | 我的博客 | 我的微信 | 我的邮箱 |
|---|---|---|---|
| baiqiantao | baiqiantao | bqt20094 | baiqiantao@sina.com |
目录
简介
区别:
instanceof是一个运算符,用来判断前面的对象是不是后面指定的类型isInstance是 Class 类的一个方法,用来判断指定的对象是否可以强转为当前类型
Class.isInstance 官方文档描述:
- 判定指定的 Object 是否与此 Class 所表示的对象赋值兼容
- 此方法是 Java 语言
instanceof运算符的动态等效方法 - 如果指定的 Object 参数
非空,且能够在不引发ClassCastException的情况下被强制转换成该 Class 对象所表示的引用类型,则该方法返回 true;否则返回 false。 - 特别地:
- 当该 Class 对象表示一个已声明的
类时,若指定的 Object 参数是所表示类(或其任一子类)的一个实例,则此方法返回 true;否则返回 false - 如果此 Class 对象表示一个
数组类,且通过身份转换或扩展引用转换,指定的 Object 参数能转换为一个数组类的对象,则返回 true;否则返回 false - 如果此 Class 对象表示一个
接口,且指定 Object 参数的类或任一超类实现了此接口,则此方法返回 true;否则返回 false - 如果此 Class 对象表示一个
基本类型,则此方法返回 false
- 当该 Class 对象表示一个已声明的
最重要的一句话:凡是 null 相关的都是 false
测试
继承关系
interface IPeron {
}
class Father implements IPeron {
}
class Child extends Father {
}
测试案例
基本测试
System.out.println("------------基本测试------------");
Child child = new Child(); //只要能强转就为 true;反过来,只要为 true,我们就能安全的强转
System.out.println(child instanceof Child && child instanceof Father && child instanceof IPeron && child instanceof Object);//true
System.out.println(Child.class.isInstance(child) && Father.class.isInstance(child) && IPeron.class.isInstance(child) && Object.class.isInstance(child));//true
测试多态
System.out.println("------------测试多态------------");
IPeron peron = new Child();
System.out.println(peron instanceof Child && peron instanceof Father && peron instanceof IPeron && peron instanceof Object);//true
测试null
System.out.println("------------测试null------------");
Father father = null; //凡是null相关的都是false
System.out.println(null instanceof Object || Object.class.isInstance(null) || father instanceof Object || Object.class.isInstance(father)); //false
Child nullChild = (Child) father; //这样是没问题的
//String nullString = (String) father; //编译不通过。Error:(39, 38) java: 不兼容的类型: Father无法转换为java.lang.String
测试数组
System.out.println("------------测试数组------------");
Child[] children = new Child[]{};
System.out.println(children instanceof Child[] && children instanceof Father[] && children instanceof IPeron[] && children instanceof Object[]);//true
System.out.println(Child[].class.isInstance(children) && Father[].class.isInstance(children) && IPeron[].class.isInstance(children) && Object[].class.isInstance(children));//true
System.out.println(new Father[]{} instanceof Child[]); //false
测试集合
System.out.println("------------测试集合------------");
List<Child> childList = new ArrayList<>();
System.out.println(childList instanceof List && childList instanceof ArrayList && List.class.isInstance(childList) && ArrayList.class.isInstance(childList));//true
测试基本类型
System.out.println("------------测试基本类型------------");
int i = 0;
Integer integer = 0;
//System.out.println(i instanceof Object);//编译不通过
System.out.println(integer instanceof Integer && integer instanceof Object);//true
System.out.println(Integer.class.isInstance(i) && Object.class.isInstance(i) && Integer.class.isInstance(integer) && Object.class.isInstance(integer)); // true
System.out.println(int.class.isInstance(i) || int.class.isInstance(integer)); // false。如果此 Class 对象表示一个基本类型,则此方法返回 false
打印结果
------------基本测试------------
true
true
------------测试多态------------
true
------------测试null------------
false
------------测试数组------------
true
true
false
------------测试集合------------
true
------------测试基本类型------------
true
true
false
2019-11-17
- instanceof 和 isInstance 强转 类型 class MD的更多相关文章
- Java中instanceof和isInstance区别详解
一次性搞定instanceof和isInstance,instanceof和isInstance长的非常像,用法也很类似,先看看这两个的用法: obj.instanceof(class) 也就是说这 ...
- java instanceof和isInstance的关系 精析
1.instanceof 用途:判断这个object对象是不是这种Class类型. 语法: boolean result = object instanceof Class; 用法: 判断obje ...
- instanceof和isInstance的区别
instanceof 是一个操作符(类似new, ==等) ( Object reference variable ) instanceof (class/interface type) if(a i ...
- Java之"instanceof"和"isInstance"代码举例
源码: /** * @Date:2018-04-20 * @Description:判断Instance * - instanceof方法返回一个boolean类型的值,意在告诉我们对象是不是某个特定 ...
- Java中instanceof和isInstance的具体区别
Java中instanceof和isInstance的具体区别 在Think in Java泛型这一章遇到这个问题,一些博客模糊提到了isInstance是instanceof的动态实现,查阅文档参考 ...
- instanceof 和isinstance的区别
class A {} class B extends A {} class C extends A {} public class Test { public static void main(Str ...
- Python中为什么推荐使用isinstance来进行类型判断?而不是type
转自:http://www.xinxingzhao.com/blog/2016/05/23/python-type-vs-isinstance.html Python在定义变量的时候不用指明具体的的类 ...
- C++枚举类型Enum及C++11强枚举类型用法
C++中的枚举类型常常和switch配合使用,这里用一个简单的switch控制键盘回调的代码片段来说明枚举的用法: //W A S D 前.后.左.右行走 enum Keydown{ Forward= ...
- C++11 强枚举类型
在标准C++11之前的枚举是继承C的,枚举类型不是类型安全的.枚举类型被视为整数,这使得两种不同的枚举类型之间可以进行比较. 一.C中enum类型的局限语法: enum type1{a, b, c}; ...
随机推荐
- es更新说明(dsl)
一.旧版elasticsearch-dsl 很多同学在python搜索引擎视频中关于看到的第十章elasticsearch使用中使用python创建mapping老师使用的以下代码,这些代码对于ela ...
- Django框架(十)--常用字段、参数、元信息、多对多关联关系
一.ORM字段 # AutoField() int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列 # IntegerField() ...
- Linux下源码编译Mysql5.7
1.安装依赖包 yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgc ...
- WPF系列 —— 控件添加依赖属性(转)
WPF系列 —— 控件添加依赖属性 依赖属性的概念,用途 ,如何新建与使用.本文用做一个自定义TimePicker控件来演示WPF的依赖属性的简单应用. 先上TimePicker的一个效果图. 概念 ...
- 异步HttpClient大量请求
由于项目中有用到HttpClient异步发送大量http请求,所以做已记录 思路:使用HttpClient连接池,多线程 public class HttpAsyncClient { private ...
- 201871010118-唐敬博《面向对象程序设计(Java)》第二周学习总结
博文正文开头格式:(3分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.co ...
- dedecms去掉标题长度限制
dedecms文章标题默认显示字数为60字节(30个汉字),如果想要显示更多要如何操作呢?两步解决问题,随ytkah一起来看看吧.1.后台找到“系统设置-系统基本参数-其他其他选项”(如下图)这时你可 ...
- opencart按重量配送如何设置
OpenCart中基于重量的配送模块让你使用自己的价格折算表来决定配送价格.可以基于不同的重量允许设置价格范围,重量和价格的格式如下:weight(重量1):cost(价格1), weight(重量2 ...
- phpcms邮箱smtp配置163企业邮测试可用
前面我们给phpcms加了https,但是修改邮箱smtp配置一直提交不了,提示请填写接口地址,格式为:http://www.abc.com,结尾不包含"/",找了一下phpsso ...
- 14-C#笔记-字符串
1. 基本操作 using System; namespace StringApplication { class Program { static void Main(string[] args) ...