用于检查某个类是否扩展了另一个类的Java方法? [英] Java Method for checking if a class extends another class?

查看:60
本文介绍了用于检查某个类是否扩展了另一个类的Java方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以检查一个类是否扩展了另一个类?上面的代码是代码段

Is there any method for checking if a one class extends another class? Above code is snippet

  File[] fileList = file.listFiles();
        if(fileList != null){
            for(int i = 0; i < fileList.length; ++i){
                ClassName = fileList[i].getName();
                if (fileList[i].isFile() && (Class Extends Class2) && ClassName.endsWith(".class")){
                    String ClassName1 = ClassName.split("\\.")[0];
                    if (!ClassName1.contains("$")){
                        if (packages != null){
                        <Snippet>
                        }
                    }
                }
            }
        }

推荐答案

假设您有 A B 类.要确定A是否扩展B,您可以使用 instanceof 运算符测试 A 的实例.这就足够了:

Let's say you have classes A and B. To determine whether A extends B you can test an instance of A with instanceof operator. This is sufficient:

if((new A()) instanceof B)
    return true;

这篇关于用于检查某个类是否扩展了另一个类的Java方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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