今天學習Spring3架構,在了解模拟實作Spring Ioc容器的時候遇到了getInterfaces()方法。getInterfaces()方法和Java的反射機制有關。它能夠獲得這個對象所實作的接口。
例如:
Class<?> string01 = person.getClass().getInterfaces()[0];
//獲得person對象所實作的第一個接口
詳細的例子如下:
Person類:
<b>[java]</b> view plain copy
print?
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!!!");
}
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);
}
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!!!");
eagle接口:
public interface eagle {
public void fly();
public interface eagle {
public void fly();
whale接口:
public interface whale {
public void swim();
public interface whale {
public void swim();
運作結果:
