每日一博 - instanceof vs isInstance vs isAssignableFrom

简介: operator instanceof;method Class.isInstance(Object obj);method Class.isAssignableFrom(Class<?> cls);

@[toc]

在这里插入图片描述


Pre

  • operator instanceof;
  • method Class.isInstance(Object obj);
  • method Class.isAssignableFrom(Class<?> cls);

isInstance

    /**
     * Determines if the specified {@code Object} is assignment-compatible
     * with the object represented by this {@code Class}.  This method is
     * the dynamic equivalent of the Java language {@code instanceof}
     * operator. The method returns {@code true} if the specified
     * {@code Object} argument is non-null and can be cast to the
     * reference type represented by this {@code Class} object without
     * raising a {@code ClassCastException.} It returns {@code false}
     * otherwise.
     *
     * <p> Specifically, if this {@code Class} object represents a
     * declared class, this method returns {@code true} if the specified
     * {@code Object} argument is an instance of the represented class (or
     * of any of its subclasses); it returns {@code false} otherwise. If
     * this {@code Class} object represents an array class, this method
     * returns {@code true} if the specified {@code Object} argument
     * can be converted to an object of the array class by an identity
     * conversion or by a widening reference conversion; it returns
     * {@code false} otherwise. If this {@code Class} object
     * represents an interface, this method returns {@code true} if the
     * class or any superclass of the specified {@code Object} argument
     * implements this interface; it returns {@code false} otherwise. If
     * this {@code Class} object represents a primitive type, this method
     * returns {@code false}.
     *
     * @param   obj the object to check
     * @return  true if {@code obj} is an instance of this class
     *
     * @since JDK1.1
     */
    public native boolean isInstance(Object obj);

isAssignableFrom

    /**
     * Determines if the class or interface represented by this
     * {@code Class} object is either the same as, or is a superclass or
     * superinterface of, the class or interface represented by the specified
     * {@code Class} parameter. It returns {@code true} if so;
     * otherwise it returns {@code false}. If this {@code Class}
     * object represents a primitive type, this method returns
     * {@code true} if the specified {@code Class} parameter is
     * exactly this {@code Class} object; otherwise it returns
     * {@code false}.
     *
     * <p> Specifically, this method tests whether the type represented by the
     * specified {@code Class} parameter can be converted to the type
     * represented by this {@code Class} object via an identity conversion
     * or via a widening reference conversion. See <em>The Java Language
     * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
     *
     * @param cls the {@code Class} object to be checked
     * @return the {@code boolean} value indicating whether objects of the
     * type {@code cls} can be assigned to objects of this class
     * @exception NullPointerException if the specified Class parameter is
     *            null.
     * @since JDK1.1
     */
    public native boolean isAssignableFrom(Class<?> cls);

isAssignableFrom()方法是从类继承的角度去判断,instanceof关键字是从实例继承的角度去判断。
isAssignableFrom()方法是判断是否为某个类的父类,instanceof关键字是判断是否某个类的子类。
父类.class.isAssignableFrom(子类.class)

子类实例 instanceof 父类类型
相关文章
|
4月前
|
并行计算 Serverless API
函数计算操作报错合集之出现 "AttributeError: 'NoneType' object has no attribute 'pop'" 错误,是什么原因
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
149 1
|
6月前
|
Java
instanceof是什么~
instanceof是什么~
|
6月前
|
Python
使用 raise_exception 装饰器,简化 if not ... raise ... 抛出异常的过程
使用 raise_exception 装饰器,简化 if not ... raise ... 抛出异常的过程
65 0
|
PyTorch 算法框架/工具
torch中报错:AttributeError: 'builtin_function_or_method' object has no attribute 'detach'怎么解决?
这个错误信息 "AttributeError: 'builtin_function_or_method' object has no attribute 'detach'" 表示你尝试在一个内置函数或方法对象上调用 detach() 方法,而这种对象没有这个属性。 detach() 是 PyTorch 张量和变量的方法,允许它们从计算图中分离出来,因此不能在其他类型的对象上调用。要解决这个错误,请确保你正在一个 PyTorch 张量或变量上调用 detach() 方法。
1099 0
|
Python
TypeError: ‘XXXX‘ object is not callable问题的一种可能性
TypeError: ‘XXXX‘ object is not callable问题的一种可能性
311 0
TypeError: ‘XXXX‘ object is not callable问题的一种可能性
|
iOS开发
Objective-C的hook方案/ Method Swizzling
Method Swizzling是改变一个selector的实际实现的技术。
108 0
|
JavaScript 前端开发
每日一题:typeof 与 instanceof 区别
每日一题:typeof 与 instanceof 区别
97 0
|
数据采集 Python
error: scrapy TypeError: 'float' object is not iterable
error: scrapy TypeError: 'float' object is not iterable
289 0
|
Android开发 开发者
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
【解决问题的思路】its super classes have no public methods with the @Subscribe annotation
1037 0