博客引用处(以下内容在原有博客基础上进行补充或更改,谢谢这些大牛的博客指导):
Java反射之getInterfaces()方法
getInterfaces()方法和Java的反射机制有关。它能够获得这个对象所实现的所有接口。
例如:
Class<?> string01 = person.getClass().getInterfaces()[0];
//获得person对象所实现的第一个接口
详细的例子如下:
Person类:
package com.deciphering.spring;
public class Person implements eagle,whale{
private String name = "小明";
private int id = 10001;
public void Speak(String name){
System.out.println("我的名字"+name+" "+ "编号"+ id);
}
@Override
public void fly() {
System.out.println("I can Fly!!!");
}
@Override
public void swim() {
System.out.println("I can swimming!!!");
}
public static void main(String args[]){
Person person = new Person();
person.Speak("小明");
person.fly();
person.swim();
System.out.println("---------------");
Class<?> string01 = person.getClass().getInterfaces()[0];
Class<Person> string02 = (Class<Person>) person.getClass().getInterfaces()[1];
System.out.println(string01);
System.out.println(string02);
}
}
eagle接口:
package com.deciphering.spring;
public interface eagle {
public void fly();
}
whale接口:
package com.deciphering.spring;
public interface whale {
public void swim();
}
运行结果: