The getInterfaces() method of Java reflection

Learning the Spring3 framework today, I encountered the getInterfaces() method when I understood the simulation implementation of the Spring Ioc container. The getInterfaces() method is related to Java's reflection mechanism. It can get the interface implemented by this object.

E.g:

Class<?> string01 = person.getClass().getInterfaces()[0];

//Get the first interface implemented by the person object

Detailed examples are as follows:

Person class:

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("My name"+name+" "+ "Number"+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 interface:

package com.deciphering.spring;

public interface eagle {
	public void fly();
}

whale interface:

package com.deciphering.spring;

public interface whale {
	public void swim();
}

operation result:





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325640345&siteId=291194637