温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java Class类中getInterfaces方法使用

发布时间:2025-11-01 02:22:49 来源:亿速云 阅读:96 作者:小樊 栏目:编程语言

在Java中,Class类的getInterfaces()方法用于获取一个类实现的接口列表。这个方法返回一个包含所有直接实现的接口的Class对象数组。如果一个类没有实现任何接口,那么返回的数组将为空。

以下是如何使用getInterfaces()方法的示例:

public interface InterfaceA {
    void methodA();
}

public interface InterfaceB {
    void methodB();
}

public class MyClass implements InterfaceA, InterfaceB {
    @Override
    public void methodA() {
        System.out.println("Method A");
    }

    @Override
    public void methodB() {
        System.out.println("Method B");
    }
}

public class Main {
    public static void main(String[] args) {
        Class<?> clazz = MyClass.class;
        Class<?>[] interfaces = clazz.getInterfaces();

        System.out.println("Interfaces implemented by MyClass:");
        for (Class<?> iface : interfaces) {
            System.out.println(iface.getName());
        }
    }
}

输出结果:

Interfaces implemented by MyClass:
InterfaceA
InterfaceB

在这个例子中,我们定义了两个接口InterfaceAInterfaceB,以及一个实现了这两个接口的类MyClass。通过调用MyClass.class.getInterfaces(),我们可以获取到MyClass实现的接口列表,并将其打印出来。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI