栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

确定类的扩展接口

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

确定类的扩展接口

使用Class.getInterfaces,例如:

Class<?> c; // Your classfor(Class<?> i : c.getInterfaces()) {     // test if i is your interface}

另外,以下代码可能会有所帮助,它将为您提供一个包含所有超类和某个类的接口的集合:

public static Set<Class<?>> getInheritance(Class<?> in){    linkedHashSet<Class<?>> result = new linkedHashSet<Class<?>>();    result.add(in);    getInheritance(in, result);    return result;}private static void getInheritance(Class<?> in, Set<Class<?>> result){    Class<?> superclass = getSuperclass(in);    if(superclass != null)    {        result.add(superclass);        getInheritance(superclass, result);    }    getInterfaceInheritance(in, result);}private static void getInterfaceInheritance(Class<?> in, Set<Class<?>> result){    for(Class<?> c : in.getInterfaces())    {        result.add(c);        getInterfaceInheritance(c, result);    }}private static Class<?> getSuperclass(Class<?> in){    if(in == null)    {        return null;    }    if(in.isArray() && in != Object[].class)    {        Class<?> type = in.getComponentType();        while(type.isArray())        { type = type.getComponentType();        }        return type;    }    return in.getSuperclass();}

编辑:添加了一些代码来获取特定类的所有超类和接口。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/574045.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号