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 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短