除了instanceof运算符之外还有什么方法可以在java中进行对象类型比较吗? [英] Is there any way other than instanceof operator for object type comparison in java?

查看:400
本文介绍了除了instanceof运算符之外还有什么方法可以在java中进行对象类型比较吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得在一些Java书中读到除了'instanceof'之外的任何运算符,用于比较两个对象之间的类型层次结构。

I remember reading in some Java book about any operator other than 'instanceof' for comparing the type hierarchy between two objects.

instanceof是最常用和常用的。我无法清楚地记得是否确实存在其他方式。

instanceof is the most used and common. I am not able to recall clearly whether there is indeed another way of doing that or not.

推荐答案

是的,有。不是运算符而是Class类的方法。

Yes, there is. Is not an operator but a method on the Class class.

这是:
isIntance(Object o)

来自doc的引用:


...此方法是Java语言instanceof的动态等价物运算符



public class Some {
    public static void main( String [] args ) {
        if ( Some.class.isInstance( new SubClass() ) ) {
            System.out.println( "ieap" );
        } else { 
            System.out.println( "noup" );
        }
    }
}
class SubClass extends Some{}

这篇关于除了instanceof运算符之外还有什么方法可以在java中进行对象类型比较吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆