zoukankan      html  css  js  c++  java
  • Java反射之getInterfaces()方法

    今天学习Spring3框架,在理解模拟实现Spring Ioc容器的时候遇到了getInterfaces()方法。getInterfaces()方法和Java的反射机制有关。它能够获得这个对象所实现的接口。

    例如:

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

    //获得person对象所实现的第一个接口

    详细的例子如下:

    Person类:

    1. package com.deciphering.spring;  
    2.   
    3. public class Person implements eagle,whale{  
    4.     private String name = "小明";  
    5.     private int id = 10001;   
    6.     public void Speak(String name){  
    7.         System.out.println("我的名字"+name+" ""编号"+ id);  
    8.     }     
    9.     @Override  
    10.     public void fly() {  
    11.         System.out.println("I can Fly!!!");       
    12.     }  
    13.       
    14.     @Override  
    15.     public void swim() {          
    16.         System.out.println("I can swimming!!!");  
    17.     }  
    18.     public static void main(String args[]){  
    19.         Person person = new Person();  
    20.         person.Speak("小明");  
    21.         person.fly();  
    22.         person.swim();  
    23.         System.out.println("---------------");  
    24.         Class<?> string01 = person.getClass().getInterfaces()[0];  
    25.         Class<Person> string02 = (Class<Person>) person.getClass().getInterfaces()[1];  
    26.         System.out.println(string01);  
    27.         System.out.println(string02);         
    28.     }  
    29. }  

    eagle接口:

    1. package com.deciphering.spring;  
    2.   
    3. public interface eagle {  
    4.     public void fly();  
    5. }  

    whale接口:

    1. package com.deciphering.spring;  
    2.   
    3. public interface whale {  
    4.     public void swim();  
    5. }  

    运行结果:





  • 相关阅读:
    阅读笔记7
    阅读笔记6
    架构阅读笔记5
    软件质量属性——易用性课堂讨论问题总结
    Git 的 .gitignore 配置
    zookeeper的简单搭建,java使用zk的例子和一些坑
    MySQL中有关TIMESTAMP和DATETIME的对比
    Mysql 如何设置字段自动获取当前时间,附带添加字段和修改字段的例子
    spring boot注入error,Consider defining a bean of type 'xxx' in your configuration问题解决方案
    net start命令发生系统错误5和错误1058的解决方法
  • 原文地址:https://www.cnblogs.com/jpfss/p/8126567.html
Copyright © 2011-2022 走看看