列出类在Java中实现的接口
可以使用java.lang.Class.getInterfaces()方法确定由对象表示的类所实现的接口。此方法返回该类实现的所有接口的数组。
演示此的程序如下所示-
示例
package Test;
import java.lang.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
listInterfaces(String.class);
}
public static void listInterfaces(Class c) {
System.out.println("The Class is: " + c.getName());
Class[] interfaces = c.getInterfaces();
System.out.println("The Interfaces are: " + Arrays.asList(interfaces));
}
}输出结果
The Class is: java.lang.String The Interfaces are: [interface java.io.Serializable, interface java.lang.Comparable, interface java.lang.CharSequence]
现在让我们了解上面的程序。
用方法listInterfaces()中的String.class调用该方法main()。演示这的代码片段如下-
listInterfaces(String.class);
在方法中listInterfaces(),该方法getName()用于打印类的名称。然后,该方法getInterfaces()用于返回该类实现的所有接口的数组。然后,使用Arrays.asList()打印此数组。演示这的代码片段如下-
System.out.println("The Class is: " + c.getName());
Class[] interfaces = c.getInterfaces();
System.out.println("The Interfaces are: " + Arrays.asList(interfaces));热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便