当前位置:网站首页>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,相当于没有元素返回。
边栏推荐
- leetcode经典例题——56.合并区间
- MindSpore:Batchnorm only support nchw input!
- 各位大佬,请问mysql数据的cdc,能指定存量数据同步的zone为utc 吗
- 注意力机制
- 获取cpu的核数
- MindSpore:mirrorpad算子速度过慢的问题
- [Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1
- TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)
- leetcode二叉树系列(一)
- 蘑菇书EasyRL学习笔记
猜你喜欢

leetcode二叉树系列(二)

冰蝎逆向初探
![[Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1](/img/6f/c736a3404377961e92b3bd1b5ea90e.png)
[Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
![一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI](/img/98/8b2359b7b99da9e50821cdaaf5e47d.png)
一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI

Win11如何隐藏输入法悬浮窗?

基于cRIO-904X搭建Simulink与Labview环境

NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】

有坦荡的远方

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

LeetCode中等题之旋转图像
随机推荐
TiCDC同步延迟问题处理
他97年的,我既然卷不过他...
LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串
MindSpore:【AIR模型导出】导出时提示源码中select_op参数类型转换失败
2022年制冷与空调设备运行操作特种作业证考试题库及模拟考试
Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]
【COS 加码福利】COS 用户实践有奖征文,等你来投稿!
Oracle怎么获取当前库或者同一台服务器上某几个库的数据总行数?
双重for循环案例以及while循环和do while循环案例
Ansible deployment scripts - pro available without pit
MindSpore:Ascend运行出现问题
MindSpore:图算融合报错
MindSpore:【mindinsight】【Profiler】用execution_time推导出来的训练耗时远小于真实的耗时
有了这篇 Kubernetes 的介绍,它的原理秒懂!
请问同一个oracle cdc表,如果flink job重新提交,是会全量读取一遍源数据还是增量呢?
函数防抖与函数节流
已解决No module named ‘flask_misaka‘【BUG解决】
LVGL的多语言转换工具--字体设置的好助手
telnet远程登录aaa模式详解【华为eNSP】
Win11不识别蓝牙适配器的解决方法