我想从实现类获取接口名称 [英] I want to get the interface name from the implementation class

查看:925
本文介绍了我想从实现类获取接口名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

List<String> list = new ArrayList<String>();
//This would give me the class name for the list reference variable.
list.getClass().getSimpleName();

我想从列表引用变量获取接口名称。
是否有任何可能的方式做到这一点?

I want to get the Interface name from the list reference variable. Is there any way possible to do that?

推荐答案

使用反射,你可以调用的 Class.getInterfaces() 方法返回一个的数组接口你的类实现。

Using Reflection you can invoke the Class.getInterfaces() method which returns an Array of Interfaces that your class implements.

System.out.println(list.getClass().getInterfaces()[0]);

要得到的只是名称

System.out.println(list.getClass().getInterfaces()[0].getSimpleName);

这篇关于我想从实现类获取接口名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆