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 官宣新年祝福语简短