集册 Java实例教程 返回指定类的继承层次结构。

返回指定类的继承层次结构。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

407
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
返回指定类的继承层次结构。


//package com.nowjava;/*来自 N o w  J a v a  . c o m*/

import java.util.ArrayList;


import java.util.Collections;


import java.util.List;


public class Main {

    public static void main(String[] argv) throws Exception {

        Class clazz = String.class;

        System.out.println(java.util.Arrays

                .toString(getInheritanceHierarchy(clazz)));

    }

    /**
    n o w j a v a . c o m - 时代Java 提供 
    **/

    /**

     * Returns the inheritance hierarchy of the specified class.

     * The returned array includes all superclasses of the specified class

     * starting with the most general superclass, which is

     * <code>java.lang.Object</code>. The returned array also

     * includes the class itself as the last element. Implemented

     * interfaces are not included.

     * @param clazz the Class

     * @return all superclasses, most general superclass first

     */

    public static Class[] getInheritanceHierarchy(Class clazz) {

        List classes = new ArrayList();

        Class currentClass = clazz;

        while (null != currentClass) {

     
展开阅读全文