Java 怎么获取由指定类(包括其本身)扩展的类和实现的接口的集合

2019年7月5日20:09:04 评论 70

1.简介

Java 怎么获取由指定类(包括其本身)扩展的类和实现的接口的集合?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 Copyright 2005-2006 Seth Fitzsimmons <[email protected]>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http:

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */


import java.util.Collection;
import java.util.HashSet;

public class Main {


    
    private static Collection<Class> getClasses(Class clazz) {
        final Collection<Class> classes = new HashSet<Class>();
        while (null != clazz) {
            classes.add(clazz);

            
            for (final Class iface : clazz.getInterfaces()) {
                classes.add(iface);
            }

            clazz = clazz.getSuperclass();
        }
        return classes;
    }
}

 

3.总结

示例代码演示了 Java 怎么获取由指定类(包括其本身)扩展的类和实现的接口的集合,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: