Java反射之getInterfaces()方法
2017-02-02 21:22
357 查看
今天学习Spring3框架,在理解模拟实现Spring Ioc容器的时候遇到了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();
}
运行结果:
例如:
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();
}
运行结果:
相关文章推荐
- Java反射之getInterfaces()方法
- Java反射实体类属性(get ,set方法)
- java反射调用set和get方法的通用类
- java反射之getGenericSuperclass方法的用处
- JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别
- Java反射获取get和set方法
- Java反射代码--05.执行所有get方法
- JAVA中的反射只获取属性的get方法
- Java反射中的getClass()方法
- java 反射直接获取get/set方法的写法
- java 反射调用Java类的私有set和get方法
- java中通过反射获取方法并且调用(getMethod和invoke深入)实践
- java使用反射调用set和get方法
- Java编程:Java的反射机制中的 getComponentType() 方法
- 使用java反射中的getMethod,invoke方法调用对象方法的实例
- java中通过反射获取方法并且调用(getMethod和invoke深入)实践
- Java反射学习(利用反射遍历链表并执行所有对象的所有get方法)
- 黑马程序员-JAVA学习之反射getDeclaredField()方法与getField()方法的区别
- java 反射机制(通过getGenericSuperclass()方法获取到父类泛型的类型)
- java 反射通过get方法获得属性值