当前位置:网站首页>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,相当于没有元素返回。
边栏推荐
- [Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
- 字符串与正则表达式(C#)
- 用OpenGL绘制winXP版扫雷的笑脸表情
- How Oracle for current library or certain library data on the same server number?
- redis解决分布式session问题
- MindSpore:损失函数问题
- cannot import name 'import_string' from 'werkzeug' [bug solution]
- JSP基本语法
- 双重for循环案例以及while循环和do while循环案例
- Get the number of cpu cores
猜你喜欢

冰蝎工具开发实现动态二进制加密WebShell

【C补充】指向指针或函数的指针

MindSpore:mirrorpad算子速度过慢的问题

如何快速将Zabbix5.0升级至6.0?

Win11不识别蓝牙适配器的解决方法

【正点原子STM32连载】第一章 本书学习方法 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1

sync-diff-inspector 使用实践

MindSpore:【mindinsight】【Profiler】用execution_time推导出来的训练耗时远小于真实的耗时

Anton Paar Anton Paar Density Meter Hydrometer Repair DMA35 Performance Parameters

TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)
随机推荐
VRRP + MSTP configuration, huawei eNSP experiment 】 【
MindSpore:MindSpore GPU版本安装问题
Redis 内存满了怎么办?这样置才正确!
路由/三层交换机DHCP下发地址详解【华为eNSP】
开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]
TiCDC同步延迟问题处理
ansible部署脚本--亲测可用无坑
参数优化。
菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿
暴力破解-破解 Apache BASIC 认证
Anton Paar安东帕密度计比重计维修DMA35性能参数
暴力破解ssh/rdp/mysql/smb服务
【云驻共创】HCSD 大咖直播–就业指南
学习在php中分析switch与ifelse的执行效率
【无标题】
Layer 3 Switch/Router OSPF Configuration Details [Huawei eNSP Experiment]
Win11怎么进行左右键对调?
Detailed explanation of switch link aggregation [Huawei eNSP]