当前位置:网站首页>Person.class.getInterfaces() 注意使用方法
Person.class.getInterfaces() 注意使用方法
2022-08-04 09:35:00 【魔道不误砍柴功】
Person.class.getInterfaces() 注意使用方法
如果此对象表示一个接口,则该数组包含表示该接口扩展的所有接口的对象。数组中接口对象顺序与此对象所表示的接口的声明的
extends
子句中接口名顺序一致。如果此对象表示一个不实现任何接口的类或接口,则此方法返回一个长度为 0 的数组。
如果此对象表示一个基本类型或
void
,则此方法返回一个长度为 0 的数组。
代码演示
package main.tcp.dubbo.test;
interface Person{
}
class Son implements Person{
}
public class TestDemo {
public static void main(String[] args) {
// 如果此对象表示一个不实现任何接口的类或接口,则此方法返回一个长度为 0 的数组。
System.out.println(Person.class.getInterfaces().length); // 输出结果是:0
System.out.println(Son.class.getInterfaces()[0]); // 输出结果是:interface main.tcp.dubbo.test.Person
ReflectInterface target = new ReflectInterfaceImpl();
System.out.println(target.getClass().getInterfaces()[0]);// 输出结果是:interface main.tcp.dubbo.test.ReflectInterface
System.out.println(new Class[]{
ReflectInterface.class}[0]);// 输出结果是:interface main.tcp.dubbo.test.ReflectInterface
System.out.println(ReflectInterface.class.getInterfaces().length); // 输出结果是:0
System.out.println(target.getClass());// 输出结果是:class main.tcp.dubbo.test.ReflectInterfaceImpl
System.out.println(ReflectInterface.class); // 输出结果是: interface main.tcp.dubbo.test.ReflectInterface
// 反射中第二个参数需要传入被代理类的接口数组,那么就有这几种写法了,如下:
// ReflectInterfaceImpl.class.getInterfaces()
// new Class<?>[]{ReflectInterface.class} // 推荐使用这种,灵活易用
// target.getClass().getInterfaces()
ReflectInterface service = (ReflectInterface)Proxy
.newProxyInstance(ReflectInterface.class.getClassLoader()
,new Class[]{
ReflectInterface.class}
,(obj, method, params) -> {
System.out.println("param="+ Arrays.asList(params));
String response = (String)method.invoke(target, params);
return response;
});
System.out.println(service.hello("我是入参"));
}
}
class ReflectInterfaceImpl implements ReflectInterface{
@Override
public String hello(String param) {
System.out.println("=================param="+param);
return "放屁";
}
}
运行结果:
0
interface main.tcp.dubbo.test.Person
interface main.tcp.dubbo.test.ReflectInterface
interface main.tcp.dubbo.test.ReflectInterface
0
class main.tcp.dubbo.test.ReflectInterfaceImpl
interface main.tcp.dubbo.test.ReflectInterface
param=[我是入参]
=================param=我是入参
放屁
Process finished with exit code 0
总结一句话,该类是否有父类接口,有的话就会有返回,否则数组长度为0,相当于没有元素返回。
边栏推荐
猜你喜欢
陈春花发布声明,这场流量狂欢该到了收尾的时候
冰蝎工具开发实现动态二进制加密WebShell
redis解决分布式session问题
Producer and Consumer Problems in Concurrent Programming
2022年制冷与空调设备运行操作特种作业证考试题库及模拟考试
蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路
下午14:00面试,14:08低着头出来了 ,问的实在是太...
2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
JSP基本语法
命里有时终须有--记与TiDB的一次次擦肩而过
随机推荐
Could you please talk about how the website is accessed?[Interview questions in the web field]
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
优炫数据库只有数据文件如何恢复
冰蝎逆向初探
Ansible deployment scripts - pro available without pit
2022 Cloud Native Computing代表厂商 | 灵雀云第三次入选Gartner中国ICT技术成熟度曲线报告
Win11文件资源管理器找不到选项卡怎么办?
【正点原子STM32连载】第三章 开发环境搭建 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
LeetCode581+621+207
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
我和 TiDB 的故事 | 缘份在,那就终是能相遇的
参数优化。
How Oracle for current library or certain library data on the same server number?
ISO14443A读卡流程(作为示例参考)
Qt:小的任务管理器(task)
Win11不识别蓝牙适配器的解决方法
三层交换机配置MSTP协议详解【华为eNSP实验】
Cloud function to achieve automatic website check-in configuration details [Web function/Nodejs/cookie]
ZbxTable 2.0 重磅发布!6大主要优化功能!
Apache APISIX 2.15 版本发布,为插件增加更多灵活性