Java如何获取由类实现的接口?
该getClass().getInterfaces()方法返回一个数组,Class该数组表示由对象实现的接口。
package org.nhooo.example.lang;
import java.util.Date;
import java.util.Calendar;
public class ClassInterfaces {
public static void main(String[] args) {
//获取Date类的实例
Date date = Calendar.getInstance().getTime();
//获取由java.util.Date类实现的所有接口,并
//打印他们的名字。
Class[] interfaces = date.getClass().getInterfaces();
for (Class iface : interfaces) {
System.out.println("Interface Name = " + iface.getName());
}
//对于原始类型,接口将为空数组。
Class c = char.class;
interfaces = c.getInterfaces();
for (Class iface : interfaces) {
System.out.println("Interface Name = " + iface.getName());
}
}
}本java.util.Date类实现了以下接口:
Interface Name = java.io.Serializable Interface Name = java.lang.Cloneable Interface Name = java.lang.Comparable
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短