Java Method for checking if a class extends another class? [duplicate]

偶尔善良 提交于 2021-01-28 08:07:19

问题


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>
                        }
                    }
                }
            }
        }

回答1:


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;


来源:https://stackoverflow.com/questions/13537183/java-method-for-checking-if-a-class-extends-another-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!